2

Consider this:

I am fetching rawbody messages by this call:

service.users().messages().get(userId='me', format='raw', id=msgid)

Then I am pushing rawbody messages by this Call:

service.users().messages().insert(userId='me', body=message)

Now when the mails contain attachments bigger 5MB, I encounter 413 "Request Entity Too Large.", and I can't push mails.

GMail API messages.insert Documentation advices to use

POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages

instead of

POST https://www.googleapis.com/gmail/v1/users/userId/messages.

But Google API Client doesn't seem to have any documentation about how to call the above Url, and it keeps getting back to latter url.

  1. How Can send post requests to first url(with /upload) with Google Api Client rather than its default?

  2. How to use /upload url and set uploadType=multipart with Google APi Client?

3

1 Answer 1

2

Yeah, this was totally unclear from the documentation for the Google Python API client, but I found the solution in this other answer. It turns out that you use the same method (users().messages().insert()) but you pass media_body instead of body['raw']. Something like this should work:

from io import BytesIO
from base64 import urlsafe_b64decode
import googleapiclient.http

b = BytesIO()
message_bytes = urlsafe_b64decode(fetched_message['raw'])
b.write(message_bytes)
media_body = googleapiclient.http.MediaIoBaseUpload(b, mimetype='message/rfc822')
service.users().messages().insert(userId='me', media_body=media_body).execute()

I haven't tried using uploadType=multipart, but maybe you can figure it out from this documentation page and looking at the contents of the googleapiclient.http module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.