Google Calendar API V3 doesn't respect timeMin timeMax parameters in nodejs Express application

I've managed to get all events between two dates as mentioned in this example on pure nodeJs environment.

But when i use same cording with nodeJs Express App, It doesn't respect the timeMin and timeMax parameters.

Instead, i get all the events from the date that i created first event on calendar. Here is my code block looks like.

function listEvents(auth) {
  var calendar = google.calendar('v3');
  calendar.events.list({
    auth: auth,
    calendarId: 'primary',

    timeMin: (new Date(Date.parse("2018-01-22"))).toISOString(),
    timeMax: (new Date(Date.parse("2018-02-27"))).toISOString(),

    singleEvents: true,
    orderBy: 'startTime'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    var events = response.items;
    if (events.length === 0) {
      console.log('No upcoming events found.');
    }
    else {
       // Do Some Stuff ...............
    }
  });
}

What am i missing here..?

Upvotes: 1

Views: 1898

Answers (2)

Rohit
Rohit

Reputation: 196

The quick start uses the older version of the google auth library. This made it work for me:

Update the libraries to the latest

npm i google-auth-library@latest

And in the quickstart file, update your code like this

let auth = google.auth;

and

let events = response.data.items;

Thanks to & modified from: https://github.com/google/google-api-nodejs-client/issues/946#issuecomment-360400714

Upvotes: 1

The starter example seems to be incorrect, but in line with the documentation.

V0.x of the google-auth-library uses the request library, so passing a querystring options does the trick:

calendar.events.list(
    {
        auth: auth,
        calendarId: 'primary',
    }, 
    { 
        qs: {
            timeMin: (new Date(Date.parse("2018-01-22"))).toISOString(),
            timeMax: (new Date(Date.parse("2018-02-27"))).toISOString(),
            singleEvents: true,
            orderBy: 'startTime'
        } 
    }, function(err, response) {
     ...

Answer url is here

Upvotes: 0

Related Questions