Reputation: 459
I've been setting up a push-notification client in node.js to watch for changes in calendars/events for a user. Setting up calendar.events.watch
works perfectly, I receive the sync
notification at myapp.com/notifications as well as all push notifications denoting changes in calendars.
Now, when needing to poll for these updated events, I found it best done by following Google's Synchronize Resources walkthrough in order to grab a syncToken
and grab events incrementally. In their java example, it makes sense how they obtain, store, and refresh the syncToken
's. However, I cannot seem to find any kind of documentation (or anyone else using them) for node.js.
How do you obtain that initial syncToken
to perform the initial sync on a calendar? Either I've been searching for the wrong keywords or the feature isn't currently supported in the node.js framework, which I would find surprising.
Here is my code for what I interpret to be the "full initial sync." This is a continuation of the Quickstart listed here.
var google = require('googleapis');
...
function listEvents(auth) {
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: 'ZZZZZZZZZZZZZZZZZZZ',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var events = response.items;
console.log(response);
});
}
The console.log(response);
statement returns...
{ kind: 'calendar#events',
etag: '"**redacted**"',
summary: '[email protected]',
updated: '2016-07-19T00:34:55.921Z',
timeZone: 'America/New_York',
accessRole: 'owner',
defaultReminders: [ { method: 'popup', minutes: 30 } ],
items:
[ { kind: 'calendar#event',
etag: '"**redacted**"',
id: '**redacted**',
status: 'confirmed',
htmlLink: 'https://www.google.com/calendar/event?eid=**redacted**',
created: '2016-07-19T00:34:55.000Z',
updated: '2016-07-19T00:34:55.715Z',
summary: 'Thing',
creator: [Object],
organizer: [Object],
start: [Object],
end: [Object],
iCalUID: '**redacted**@google.com',
sequence: 0,
reminders: [Object] } ] }
Should that nextSyncToken
be found in here?
Upvotes: 4
Views: 4368
Reputation: 36
I found that the response does contain 'nextSyncToken' only when the query parameter 'orderBy' is excluded from the request.
All of the unsupported query parameters for using sync tokens are documented at https://developers.google.com/calendar/v3/reference/events/list under the headline nextSyncToken.
Upvotes: 1
Reputation: 707258
You do an initial full sync (which does not require a syncToken) and as part of that full sync response, you get all the current calendar items and (on the last page if the results are paged) you get a field called nextSyncToken
. You then use that nextSyncToken
value for the next incremental sync.
From the doc on initial full sync:
In the response to the list operation, you will find a field called nextSyncToken representing a sync token. You'll need to store the value of nextSyncToken. If the result set is too large and the response gets paginated, then the nextSyncToken field is present only on the very last page.
You have to store the sync token until you do the next incremental sync and you present that token as part of the incremental sync request. When you do the next incremental sync, it will then give you a new syncToken
which you will again store and use for the next incremental sync.
This syncToken
is how Google knows which change events to send you since your last sync.
Upvotes: 4