在Google API客户端中使用/upload网址 [英] using /upload urls with Google API client

查看:104
本文介绍了在Google API客户端中使用/upload网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:

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

I am fetching rawbody messages by this call:

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

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)

现在,当邮件包含大于5MB的附件时,我会遇到413请求实体太大",并且无法推送邮件.

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文档的建议使用

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

代替

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

但是Google API客户端似乎没有有关如何调用上述网址的任何文档,并且一直在返回后一个网址.

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. 如何使用Google Api Client(而不是默认值)将发帖请求发送到第一个网址(带有/upload)?

  1. How Can send post requests to first url(with /upload) with Google Api Client rather than its default?

如何在Google APi Client中使用/upload url并设置uploadType = multipart?

How to use /upload url and set uploadType=multipart with Google APi Client?

推荐答案

是的,对于Google Python API客户端的文档来说,这还不是很清楚,但是我在

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

我还没有尝试使用uploadType=multipart,但是也许您可以从

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.

这篇关于在Google API客户端中使用/upload网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆