我目前正在编写一个小的python脚本,它可以与Gmail-API一起使用。我正在尝试向gmail服务器发送批处理请求。由于gmail发送批量请求的原生方法在2020年8月变得过时,我不得不自己构建一个。它的形式必须是“multipart/mixed”。
根据文档,它必须看起来像这样(https://developers.google.com/gmail/api/guides/batch):

我尝试使用python请求库,但它似乎只支持'multipart/ form‘形式的请求。但我不确定。
# 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请求?
提前感谢!
发布于 2021-10-31 23:54:53
您可以将每个请求添加到批处理中,然后执行批处理。例如:
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,您现在可以访问它们-这将需要一些字符串解析(我使用了上一个)。
https://stackoverflow.com/questions/67658283
复制相似问题