我有一个在用户Gmail中查找新邮件的流程.如果消息满足特定的地址条件,则会将消息添加到外部数据库.
我们一直在使用Users.History.List,它返回对它们进行了更改的所有消息.这是非常低效的,因为我们必须随后检查每条消息以查看我们是否已经处理过它.
我们正在考虑使用Users.Messages.List并检查MsgId以查看它是否大于先前的检查(我们存储Id).这里假设MsgId将继续变大.这种做法有缺陷吗?别人在做什么?
非常感谢.
以下是如何仅获取新消息的示例(使用Java客户端API)
List<History> histories = new ArrayList<History>();
ListHistoryResponse response = service.users().history().list(userUID).setStartHistoryId(startHistoryId).execute();
//get all history events and not only the first page
while (response.getHistory() != null) {
histories.addAll(response.getHistory());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = service.users().history().list(userUID)
.setPageToken(pageToken)
.setStartHistoryId(startHistoryId).execute();
} else {
break;
}
}
//for each history element find the added messages
for (History history : histories) {
List<HistoryMessageAdded> addedMessages = history.getMessagesAdded();
if (addedMessages == null){
continue;
}
//call to message.get for each HistoryMessageAdded
for (HistoryMessageAdded addedMsg : addedMessages) {
Message message = addedMsg.getMessage();
Message rawMessage = service.users().messages().get(userUID, message.getId()).setFormat("raw").execute();
}
}
Run Code Online (Sandbox Code Playgroud)
可能在其他语言/ REST API中有类似的实现.
您可以使用其他历史事件,例如:messagesDeleted,labelsAdded和labelsRemoved参考:https: //developers.google.com/gmail/api/v1/reference/users/history/list
小智 3
消息ID是唯一的,其值永远不会改变。要获取新消息,您可以使用history.list(),并给出您之前为该消息设置的historyId最大消息historyId。
以下是响应示例:
{
"history": [
{
"id": "1825077",
"messages": [
{
"id": "14b4c0dbc6ba9a57",
"threadId": "14b4b4ae8cfbea5c"
}
]
},
{
"id": "1825087",
"messages": [
{
"id": "14b4c0dc3ab5e49b",
"threadId": "14b4b4ae8cfbea5c"
}
]
},
{
"id": "1825097",
"messages": [
{
"id": "14b4c0e07e0f6545",
"threadId": "14b4b4ae8cfbea5c"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
historyId1825097 是消息“14b4c0e07e0f6545”中最大的。这里也Msgid没有改变,只是历史记录 id 发生了变化。
如果您提供 1825097 作为历史 ID,并且消息中没有更改,则响应将为带有标头的 200。如果收到响应 404 错误,则需要使用messages.list()来执行完全同步。
| 归档时间: |
|
| 查看次数: |
2632 次 |
| 最近记录: |