using /upload urls with Google API client

陌路散爱 提交于 2020-01-04 14:37:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31878549/using-upload-urls-with-google-api-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!