GMail API获取线程的最后一条消息

2024-06-29 00:27:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python/googleappengine上使用gmailapi。我有一个查询返回某些线程id,现在我想得到每个线程的最后一条消息。由于结果不一定按日期排序,我想知道什么是最有效的API调用?在

根据下面的注释,我设置了以下批处理函数:

if threads != []:
    count = 0 #start a new batch request after every 1000 requests
    batch = BatchHttpRequest(callback=get_items)
    for t in threads:
        batch.add(service.users().threads().get(userId=email, id=t), request_id=some_id)
        count += 1
        if count % 1000: #batch requests can handle max 1000 entries
            batch.execute(http=http)
            batch = BatchHttpRequest(callback=get_items) 
    if not count % 1000:
            batch.execute(http=http)

然后执行get_items,它运行以下逻辑来确定线程中的最后一封电子邮件是否是已发送的项目。在

^{pr2}$

这似乎适用于大多数情况,但是,在某些情况下,上面创建的“item”包含2个具有不同historyID的消息。不知道是什么原因造成的,我想知道之前只是创造一个解决办法。。。在


Tags: idhttp消息executegetifrequestcount
1条回答
网友
1楼 · 发布于 2024-06-29 00:27:53

gmailapi现在支持字段internalDate。在

internalDate - The internal message creation timestamp (epoch ms), which determines ordering in the inbox.

在线程中获取最新消息并不比用户.thread:get请求,询问单个消息的id和internalDate,并找出最后创建的消息。在

fields = messages(id,internalDate)

GET https://www.googleapis.com/gmail/v1/users/me/threads/14e92e929dcc2df2?fields=messages(id%2CinternalDate)&access_token={YOUR_API_KEY}

回应:

^{pr2}$

相关问题 更多 >