Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a users mailbox current history Id

Tags:

gmail-api

I'd like to start syncing a users mailbox going forward so I need the most recent historyId of the users mailbox. There doesn't seem to be a way to get this with one API call.

The gmail.users.history.list endpoint contains a historyId which seems to be what I need, from the docs:

historyId   unsigned long   The ID of the mailbox's current history record.

However to get a valid response from this endpoint you must provide a startHistoryId as a parameter.

The only alternative I see is to make a request to list the users messages, get the most recent history id from that, then make a request to gmail.users.history.list providing that historyid to get the most recent one.

Other ideas?

like image 345
aloo Avatar asked Sep 30 '22 16:09

aloo


2 Answers

Did you check out https://developers.google.com/gmail/api/guides/sync ?

Depending on what your use-case is, to avoid races between your current state and when you start to forward sync, you'll need to provide an appropriate historyId. If there were a "get current history ID" then anything between your previous state and when you got those results would be lost. If you don't have any particular existing state (e.g. only want to get updates and don't care about anything before that) then you can use any historyId returned (e.g. on a message or thread) as you mention.

like image 90
Eric D Avatar answered Oct 04 '22 03:10

Eric D


Small example for C# users (mentioned in comments by @EricDeFriez).

Nuget package Google.Apis.Gmail.v1 must be installed. See also quickstart for .NET developers.

var service = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

var req = service.Users.GetProfile("me");
req.Fields = "historyId";
var res = req.Execute();

Console.WriteLine("HistoryId: " + res.HistoryId);
like image 44
opewix Avatar answered Oct 04 '22 02:10

opewix