首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中向Gmail-API发送批处理请求?

如何在python中向Gmail-API发送批处理请求?
EN

Stack Overflow用户
提问于 2021-05-23 17:39:53
回答 1查看 217关注 0票数 2

我目前正在编写一个小的python脚本,它可以与Gmail-API一起使用。我正在尝试向gmail服务器发送批处理请求。由于gmail发送批量请求的原生方法在2020年8月变得过时,我不得不自己构建一个。它的形式必须是“multipart/mixed”。

根据文档,它必须看起来像这样(https://developers.google.com/gmail/api/guides/batch):

我尝试使用python请求库,但它似乎只支持'multipart/ form‘形式的请求。但我不确定。

代码语言:javascript
复制
# A list with all the message-id's.
messages = tqdm(gmail.list_messages('me'))

gmailUrl = "https://gmail.googleapis.com/batch/gmail/v1"

request_header = {"Host": "www.googleapis.com", "Content-Type": "multipart/mixed", "boundary"="bound"}

request_body = ""

# I want to bundle 80 GET requests in one batch.
# I don't know how to proceed from here.
for n in range(0,80):


response = req.post(url=gmailUrl, auth=creds, headers=request_header, files=request_body)

print(response)

所以我的问题很简单:

如何使用python向Gmail-API发送一个带有“multipart/mixed”格式的http请求?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-10-31 23:54:53

您可以将每个请求添加到批处理中,然后执行批处理。例如:

代码语言:javascript
复制
response = service.users().threads().list(userId="me").execute()
bt=service.new_batch_http_request()
for thread in threads:
    bt.add(service.users().threads().get(userId="me",id=thread["id"]))
bt.execute()

bt对象现在将有字段_requests和_responses,您现在可以访问它们-这将需要一些字符串解析(我使用了上一个)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67658283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档