使用带有Gmail API的Resumable上传附件文件 [英] Attaching a file using Resumable upload w/ Gmail API

查看:207
本文介绍了使用带有Gmail API的Resumable上传附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Gmail的Resumable选项将附件上传到电子邮件。文档参考: https://developers.google.com/gmail/api/guides / uploads#resumable



目前我可以发送带有可恢复URI的电子邮件,但没有附件(使用邮递员)。文档没有提供非常明确的例子,说明请求具体应该是什么样的,并且在淘宝网之后似乎没有很多例子。



我的请求是分为两部分:


  1. 初始请求 -

     请求URL:
    POST / upload / gmail / v1 / users / me / messages / send?uploadType =可恢复
    主机:www.googleapis.c om所以我打断了url)

    标题:
    授权:承载者my_token_here
    Content-Length:113
    Content-Type:application / json
    X-上传内容长度:67
    X上传内容类型:message / rfc822

    正文:
    {raw:VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4}


正文是一个64位编码的字符串,和电子邮件内容。然后,gmail返回一个空的正文响应,并显示如下所示的位置标题:googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=resumable&upload_id=BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k- KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E。 (删除了https://,因为这个账户只能在帖子中有一个链接)。

然后我跟进一个PUT请求到位置标题中返回的那个URL。


  1. 第二个请求如下所示:

     请求URL:?
    PUT /上传/ Gmail的/ V1 /用户/ ME /消息/发送uploadType =可恢复&安培; upload_id = BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k-KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E
    主机:www.googleapis.c om

    标题:
    Content-Length:67
    Content-Type:message / rfc822

    正文:
    {raw:VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4}
    --- OR ---
    我选择二进制选项,并附加我想通过Postman上传的文件。


我收到Gmail提供的回复像这样:

  {
id:159d7ded3125e255,
threadId:159d7ded3125e255 ,
labelIds:[
SENT
]
}

电子邮件已成功发送,但没有电子邮件附件。当我在Gmail中显示原始电子邮件时,没有任何附件的证据。原始内容如下所示:

 收到:325276275830,由gmailapi.google.com使用HTTPREST命名为unknown; 2017年1月25日星期三15:03:33 -0800 
收件人:some.name@gmail.com
主题:TestEmailSubject3
日期:2017年1月25日星期三15:03:33 -0800
Message-Id:< CEROA6F = 0ohk33RD9XyC_gW1DZO88xYF4bXYqrCSct62MUuytDw@mail.gmail.com>
来自:name_here@gmail.com

信息内容#3。

我错过了什么?我是否需要以不同的方式对某些特定内容进行编码,或将某些数据放在不同的位置?我没有收到任何错误。我一直在为此工作了几天,我无法弄清楚。

解决方案

我遇到同样的问题,我通过使用Nodemailer创建具有附件的电子邮件,将结果保存到文件,然后使用这个


I am attempting to use Gmail's Resumable option for uploading attachments to an email. Documentation reference: https://developers.google.com/gmail/api/guides/uploads#resumable.

Currently I am able to send the email with the resumable URI, but without an attachment (using Postman). Documentation doesn't provide very clear examples of what the request should specifically look like, and there don't seem to be many examples after scouring the internet.

My requests are in two parts:

  1. Initial request -

    Request URL:
    POST /upload/gmail/v1/users/me/messages/send?uploadType=resumable
    Host: www.googleapis.c om (can't post links so I interrupted the url)
    
    Headers: 
    Authorization: Bearer my_token_here
    Content-Length: 113
    Content-Type: application/json
    X-Upload-Content-Length: 67
    X-Upload-Content-Type: message/rfc822
    
    Body:
    {"raw":"VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4"}
    

The body is a 64bit encoded string that include the To, Subject, and email message contents. Then gmail returns a response with an empty body, and a 'location' header that looks like the following: googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=resumable&upload_id=BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k-KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E. (removed https:// because this account can only have one link in a post)

I then follow up with a PUT request to that URL returned in the location header.

  1. The second request looks like the following:

    Request URL: 
    PUT /upload/gmail/v1/users/me/messages/send?uploadType=resumable&upload_id=BRnB2UoAsKwzNMoQAy-JtmP6mu5agltqOWZ9uerI3k-KNTDJ73PWEjKuAHpko4RN6weSEysddH2kjj4G24uFw6E9oPv1XP69l7_KcmNuW-RAoz_5oS1T_4_E
    Host: www.googleapis.c om
    
    Headers: 
    Content-Length: 67
    Content-Type: message/rfc822
    
    Body:
    {"raw":"VG86IG5pcnZhbmEucm9ja2VyQGdtYWlsLmNvbQpTdWJqZWN0OiBUZXN0RW1haWxTdWJqZWN0MwoKTWVzc2FnZSBjb250ZW50cyAjMy4"}
    --- OR ---
    I choose the binary option, and attach the file I am looking to upload via Postman.
    

I receive a response from Gmail with an object like this:

    {
      "id": "159d7ded3125e255",
      "threadId": "159d7ded3125e255",
      "labelIds": [
        "SENT"
      ]
    }

And an email is sent successfully, however there isn't an attachment with the email. When I show the original email in Gmail, there isn't any evidence of an attachment. Original looks like the following:

    Received: from 325276275830 named unknown by gmailapi.google.com with HTTPREST; Wed, 25 Jan 2017 15:03:33 -0800
    To: some.name@gmail.com
    Subject: TestEmailSubject3
    Date: Wed, 25 Jan 2017 15:03:33 -0800
    Message-Id: <CEROA6F=0ohk33RD9XyC_gW1DZO88xYF4bXYqrCSct62MUuytDw@mail.gmail.com>
    From: name_here@gmail.com

    Message contents #3.

What am I missing? Do I need to encode some particular contents in a different way, or put some data in a different location? I'm not receiving any errors. I've been working on this for a few days now and I just can't figure it out.

解决方案

I ran into the same problem, i made it work by using Nodemailer to create the email with the attachments, save the result to a file, then upload it with this.

这篇关于使用带有Gmail API的Resumable上传附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆