考虑一下:
我正在通过此调用获取原始消息:
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使用建议
发布 https://www.googleapis.com/upload/gmail/v1/users/userId/messages
而不是
发布 https://www.googleapis.com/gmail/v1/users/userId/messages
。
但是Google API Client似乎没有任何关于如何调用上述URL的文档,并且它不断返回到后一个URL。
如何使用 Google Api 客户端而非默认客户端将发布请求发送到第一个网址(带有/upload)?
如何通过 Google APi 客户端使用/upload url 并设置 uploadType=multipart?
最佳答案
是的,从 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
模块的内容。
关于gmail-api - 通过 Google API 客户端使用/上传网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31878549/