gmail-api - 通过 Google API 客户端使用/上传网址

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

考虑一下:

我正在通过此调用获取原始消息:

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。

  1. 如何使用 Google Api 客户端而非默认客户端将发布请求发送到第一个网址(带有/upload)?

  2. 如何通过 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/

相关文章:

google-apps-script - 如何获取所有附有特定标签的草稿消息

django - Google Oauth 2.0 网络应用程序 "Authorized redirect URIs"必须以公共(public)顶级域(例如 .com 或 .org)结尾?

pip 错误 : no matching distribution found for google-api-python-client

python - 为什么我使用 Translate API 的 Python App Engine 应用程序收到 ImportError : No module named apiclient. 发现错误?

c# - 无法以正确的格式对特殊字符电子邮件进行编码,gmail api & ae.net.mail

javascript - 如何使用 Gmail 的 Node.js API 设置收件人

google-app-engine - Gmail REST API 线程搜索未给出预期结果

google-api - 如果用户登录多个帐户,如何在给定线程 ID 的情况下可靠地链接到 Gmail 对话?

python - 使用 PyDrive 管理来自公共(public) Google 云端硬盘 URL 的文件

python - 适用于 Python 的 Google API 客户端。批量请求: how to access to a specific request in the callback