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?

inderjits
  • 21
  • 2
  • Try the [Media Upload docs](https://developers.google.com/api-client-library/python/guide/media_upload), I'm not sure if it supports Gmail but I think it should. – abraham Aug 07 '15 at 14:24
  • 1
    Media Upload docs above are correct and yeah it does support Gmail. – Eric D Aug 07 '15 at 18:59
  • [here is how to insert/send message with large file, pythons code.](http://stackoverflow.com/a/38910991/5423664) – Ilan Laloum Aug 12 '16 at 06:10

1 Answers1

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.

Community
  • 1
  • 1
Avril
  • 813
  • 1
  • 9
  • 20