GMail API: Fastest way to get the oldest email in inbox

依然范特西╮ 提交于 2019-12-08 03:19:09

问题


I want to get the oldest email in GMail inbox. How can I do that with the minimum number of API calls?

One way to do it could be to get the total number of emails from Users:getProfile API and then use User.messages:list API to get the last page using "pageToken" query parameter by using the formula

pageToken = totalMessages/50

to get the list of messages from the last page of my inbox and then using the mid of the last message in the list to fetch the oldest email.

I am just wondering if there is a better way to do this? I could not figure out other ways to do it from the documentation. Any search filter that will fetch me the oldest email?


回答1:


User.messages.list contains a paramter called q which can be used for searching.

https://www.googleapis.com/gmail/v1/users/userId/messagesq=before%3A2008%2F01%2F01&access_token={token}

Basicly before:2008/01/01 is sent just like searching in the gmail web app return all the emails before that date.

The trick here will be narrowing down your search. Gmail was released on 2004. Which gives yo possible start between 2018 and 2004 split it in half

before:2011/01/01  (there were mails split it in half again) 
before:2007/01/01  (there were no mails must be between 11 and 7)
before:2009/01/01  (...)

There is a term for this method of searching but i cant remember it right now. Basically you keep dividing it by two and finding out where the result must be.



来源:https://stackoverflow.com/questions/49049234/gmail-api-fastest-way-to-get-the-oldest-email-in-inbox

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