-->

Attaching a file using Resumable upload w/ Gmail A

2020-04-10 01:30发布

问题:

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.

回答1:

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.