wanasit / google-calendar

Google Calendar API connection in Node.js
MIT License
230 stars 54 forks source link

Events list request limited to 2500 ? #6

Closed martindaniel4 closed 11 years ago

martindaniel4 commented 11 years ago

Hi there,

I try to count all events from a calendar by day. To do so I need to retrieve all events from a calendar.

I set maxResults parameter up to 10 000 but I still get only 2 500 events.

Have you experienced this issue before ?

Thanks,

Martin

martindaniel4 commented 11 years ago

Hmm, I am sure there is something to do with this pageToken thing :) http://googleappsdeveloper.blogspot.fr/2012/01/reading-query-results-from-calendar-in.html

wanasit commented 11 years ago

Hi, I've never reached the limit, but 2500 events sound reasonable for the API.

And, yes. To get more than 2500 events, you need to use pageToken.

Ref. https://developers.google.com/google-apps/calendar/v3/reference/events/list https://developers.google.com/google-apps/calendar/v3/reference/events/list#try-it

If you get nextPageToken inside the response, you can use it to get the next 2500 events.


google_calendar.events.list(calendarId, { maxResults:2500 }, function(err, data) {

    if(data.nextPageToken){

      //get the next 2500 events
      google_calendar.events.list(calendarId, {maxResults:2500, pageToken:data.nextPageToken}, function(err, data) {

      })
    }
})
martindaniel4 commented 11 years ago

Great, thanks for your quick response : I can now sucessfully get up to 5000 events.

Do you see a way to make this function a bit more generic, retrieving all events of a calendar ?

I cannot make it work using the while loop described in the example..

wanasit commented 11 years ago

You can use recursive function.

Note: I haven't tried the code. But I think you got an idea.

function fetchEventWithToken(nextPageToken){

  var options = {
    maxResults:2500,
    pageToken:nextPageToken
  }

  //Fetch events
  google_calendar.events.list(calendarId, options, function(err, data) {

    //If there is more result, call the function again...
    if(data.items.length >= 2500 && data.nextPageToken ){
      fetchEventWithToken(data.nextPageToken)
    }

    //Do something with events
    console.log(data.items)
  })
}

//Start the function
fetchEventWithToken(null)
martindaniel4 commented 11 years ago

Great ! Thanks for your help.