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

查看:234
本文介绍了如何使用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).

我看过Push通知和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天全站免登陆