使用/上传网址与Google API客户端

时间:2015-08-07 13:08:33

标签: gmail-api google-api-python-client

考虑一下:

我通过此调用获取rawbody消息:

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

然后我通过此呼叫推送rawbody消息:

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

现在当邮件包含大于5MB的附件时,我遇到413“请求实体太大了。”,我无法推送邮件。

GMail API messages.insert Documentation建议使用

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

而不是

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

但Google API客户端似乎没有关于如何调用上述Url的任何文档,并且它会不断回到后一个URL。

  1. 如何使用Google Api客户端发送帖子请求到第一个网址(带/上传)而非默认值?

  2. 如何使用/上传网址并使用Google APi客户端设置uploadType = multipart?

1 个答案:

答案 0 :(得分:2)

是的,这在Google Python API客户端的文档中完全不清楚,但我在this other answer中找到了解决方案。事实证明,您使用相同的方法(users().messages().insert()),但是您传递了media_body而不是body['raw']。这样的事情应该有效:

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()

我还没有尝试使用uploadType=multipart,但也许你可以从this documentation page找出来并查看googleapiclient.http模块的内容。