0

I have a web page that uses gmail API and javascript to deal with gmail messages. I am stuck at a point where I want to retrieve the messages after a particular timestamp.

This is part of my code that lists the mails

{
var request = gapi.client.gmail.users.messages.list({
      'userId': 'me',
      'maxResults': 10

 });

I want to list the messages after the longdate 1531958459000.

My attempt was as below

{
var request = gapi.client.gmail.users.messages.list({
      'userId': 'me',
      'after':'1531958459000',
      'maxResults': 10

 });

and it doesn't seem to work. Instead getting messages after that time, I am getting 10 messages at random, like the code without the after part.

Any help?

Neo
  • 755
  • 1
  • 10
  • 24
  • try this var request = gapi.client.gmail.users.messages.list({ 'userId': 'me', 'maxResults': {maxResult:'10'}, }); – Amine Ramoul Jul 19 '18 at 14:05
  • @Amine Ramoul, The question is about getting messages after a particular timestamp, your suggestion does not answer it; sorry. – Neo Jul 19 '18 at 16:00
  • i know that the max Result doesn't work yet, it'll take a lot of time for recurcivity if you init it, so you are not looking at the right place look at this post : https://github.com/google/google-api-nodejs-client/issues/469 – Amine Ramoul Jul 19 '18 at 17:04

2 Answers2

0

There is two issues with your code here.

First, you can't put the field after in the request body. However, you have the q field which can be used to input Gmail queries, just as in the Gmail app. Check the documentation for more details: https://developers.google.com/gmail/api/v1/reference/users/messages/list

Second, it seems that the after query doesn't support timestamp in milliseconds, but rather in seconds.

So, your code should look like this:

var request = gapi.client.gmail.users.messages.list({
  'userId': 'me',
  'q':'after:1531958459',
  'maxResults': 10
});

Best regards!

frankie567
  • 1,703
  • 12
  • 20
0

I figured it out. Basically, the additional query parameters accept 'q': as a query and any parameters. These parameters should match the format of a regular gmail inbox search according to google's documentation.

So I had to change the long date into yyyy/mm/dd format. I did it using the hint from this answer.

And then amended my code like below:

{
var ld = new Date(lastupdatedtime).toJSON().slice(0, 10).split("-").reverse().join("/");//lastupdated time in google search query string like format
var cd = new Date(+ new Date()).toJSON().slice(0, 10).split("-").reverse().join("/");//the current time in google search query string like format
var search_query = "after:"+ld+" before:"+cd;
var request = gapi.client.gmail.users.messages.list({
      'userId': 'me',          
      'q': search_query,
      'maxResults': 10//the number of results to check through

    });

and it worked well.

Neo
  • 755
  • 1
  • 10
  • 24