gpt4 book ai didi

gmail-api - 批量获取消息性能

转载 作者:行者123 更新时间:2023-12-03 18:11:09 32 4
gpt4 key购买 nike

我需要获取收件箱中的最后 100 条消息(仅限标题)。为此,我目前正在使用 IMAP 扩展来搜索然后获取消息。这是通过两个请求完成的( SEARCH 然后是 UID FETCH )。
相当于在一个请求中获取多封邮件的 Gmail API 是什么?
我能找到的只是一个批处理 API,它看起来更麻烦(组成一长串 messages:get 请求,包裹在纯 HTTP 代码中)。

最佳答案

Gmail API 中的内容与 IMAP 中的内容几乎相同。两个请求:第一个是 messages.list 来获取消息 ID。然后一个(批处理)message.get 来检索你想要的。根据您使用的语言,客户端库可能有助于批量请求构建。

A batch request is a single standard HTTP request containing multiple Google Cloud Storage JSON API calls, using the multipart/mixed content type. Within that main HTTP request, each of the parts contains a nested HTTP request.



发件人: https://developers.google.com/storage/docs/json_api/v1/how-tos/batch

这真的不难,即使没有 python 客户端库(仅使用 httplib 和 mimelib),我也花了大约一个小时在 python 中弄清楚。

这是执行此操作的部分代码片段,再次使用直接 python。希望它清楚地表明并没有涉及太多:
msg_ids = [msg['id'] for msg in body['messages']]
headers['Content-Type'] = 'multipart/mixed; boundary=%s' % self.BOUNDARY

post_body = []
for msg_id in msg_ids:
post_body.append(
"--%s\n"
"Content-Type: application/http\n\n"
"GET /gmail/v1/users/me/messages/%s?format=raw\n"
% (self.BOUNDARY, msg_id))
post_body.append("--%s--\n" % self.BOUNDARY)
post = '\n'.join(post_body)
(headers, body) = _conn.request(
SERVER_URL + '/batch',
method='POST', body=post, headers=headers)

关于gmail-api - 批量获取消息性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439764/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com