How to check for incoming new messages using Gmail API

大兔子大兔子 提交于 2019-12-01 10:38:18

问题


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).

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.


回答1:


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:

Request

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}

Response

{
 "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.



来源:https://stackoverflow.com/questions/33467106/how-to-check-for-incoming-new-messages-using-gmail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!