如何检测是否已通过gmail api将标签添加到电子邮件? [英] How to detect if label was added to email via gmail api?

查看:68
本文介绍了如何检测是否已通过gmail api将标签添加到电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试检测电子邮件是否带有一个特定标签.按照文档此处进行反序列化推送消息并获得了historyId 例如. 390100,但事实证明我无法获得确切的390100历史记录条目.

I try to detect if email message got one particular label. Following the documentation here I do deserialize push message and got historyId eg. 390100 but it turns out I can't get exact 390100 history entry.

history.list api具有startHistoryId参数,并且文档指出您在之后获得该historyId.

history.list api has startHistoryId param and docs states that you get all history after that historyId.

但是,更糟糕的390100历史记录事件为空(非常好!). 标签收藏的真实条目是之前推送通知中的historyId,例如在未发布

However what is even worse 390100 history event is empty (sic!). The real entry with labelsAdded was before that historyId in push notification eg. at 39086 which is not published

试图解决,并减去例如. 15表格390100并使用startHistoryId 39085来进行history.list

Tried to workaround and subtract eg. 15 form 390100 and do history.list with startHistoryId 39085

如果我很幸运并且39085仍然存在,我将不会得到404,并最终获得有关我的标签已添加到邮件中的信息.但这对我来说似乎是个坏习惯.

If I'm in luck and 39085 still exists I will not get 404 and eventually got information that my label was added to message. But that seems like a bad hack for me.

有什么可靠的方式知道添加历史标签的哪一点,并且有可靠的方式向后搜索历史吗?

Is there any reliable way to know at which point in history label was added, and is there reliable way to search history backwards ?

我还交叉发布了rant/issue

I also cross posted rant/issue here

推荐答案

在推送通知中获得的historyId只是表示当前时间.因此,如果在询问历史记录时使用此historyId,将不会获得任何结果.请改用此工作流程:

The historyId you get in the push notification simply represents the current time. So if you use this historyId when asking for history, you will not get any results. Use this workflow instead:

  1. 保存historyId.
  2. 当您进行推送时,请使用在步骤1中保存的historyId,您将获得所有相关的更改.
  3. 保存您在推送通知中获得的hisotryId,并在下次使用.
  4. 重复步骤2.
  1. Save a historyId.
  2. When you get a push, use the historyId you saved in step 1, and you will get all the relevant changes.
  3. Save the hisotryId you got in the push notification, and use it next time.
  4. Repeat step 2.

示例

  1. 我从 getProfile 开始. -操作:
  1. I get a starting historyId with the getProfile-operation:

请求

GET https://www.googleapis.com/gmail/v1/users/me/profile?fields=historyId&access_token={YOUR_API_KEY}

回复

{
 "historyId": "655156"
}

  1. 我得到了推动,并列出历史记录自上次以来:
  1. I get the push, and list the history since last time:

请求

GET https://www.googleapis.com/gmail/v1/users/me/history?labelId=STARRED&startHistoryId=655156&fields=history%2FlabelsAdded&key={YOUR_API_KEY}

回复:

{
 "history": [
  {
   "labelsAdded": [
    {
     "message": {
      "id": "151237ed0d001368",
      "threadId": "151237ed0d001368",
      "labelIds": [
       "STARRED",
       "CATEGORY_UPDATES",
       "INBOX"
      ]
     },
     "labelIds": [
      "STARRED"
     ]
    }
   ]
  }
 ]
}

  1. 保存我推送的historyId,并在下次使用.
  1. Save the historyId I got in the push, and use that next time.

这篇关于如何检测是否已通过gmail api将标签添加到电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆