如何使用 Gmail API 检查收到的新邮件 [英] How to check for incoming new messages using Gmail API

查看:33
本文介绍了如何使用 Gmail API 检查收到的新邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个可以从 Gmail 帐户中提取数据的 python 脚本,但我想将其设置为自上次调用 API 以来它只会提取新消息(我将 ping服务器定期).

I have set up a python script that can pull data from Gmail account, but I would like to set it up in a way that it would only pull new message since the last time I made the API call (I will be pinging the server regularly).

我查看了推送通知和 Pub/Sub,但我不太确定这些是否相关,或者我应该查看其他内容.Gmail 也有 Users.history: list 功能,但我想知道这是否可以以任何有用的方式使用.

I have looked at Push notification and Pub/Sub, but I am not quite sure if these are relevant or I should be looking at something else. Gmail also has Users.history: list function, but I am wondering if this can be used in any useful way.

推荐答案

你可以 列出消息 就像你通常做的那样,但是说你想要某个时间戳之后的消息.这样,您可以轮询新消息,例如每分钟,给出你最后一次检查消息的时间,以 自纪元以来的秒数:

You could list messages as you usually do, but saying that you want messages after a certain timestamp. This way, you can poll for new messages e.g. every minute, giving the last time you checked for messages in seconds since the epoch:

请求

q = is:unread AND after:<time_since_epoch_in_seconds>

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=is%3Aunread+AND+after%3A1446461721&access_token={YOUR_API_KEY}

回应

{
 "messages": [
  {
   "id": "150c7d689ef7cdf7",
   "threadId": "150c7d689ef7cdf7"
  }
 ],
 "resultSizeEstimate": 1
}

然后你只保存发出请求时的时间戳,一分钟后使用这个时间戳.

Then you just save the timestamp when you issued the request, and use this timestamp one minute later.

这篇关于如何使用 Gmail API 检查收到的新邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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