将资源与 Google 日历同步 node.js

Synchronize Resources with Google Calendar for node.js

背景

我一直在 node.js 中设置推送通知客户端,以监视用户在 calendars/events 中的变化。设置 calendar.events.watch 完美运行,我在 myapp.com/notifications 收到 sync 通知以及所有表示日历更改的推送通知。

问题

现在,当需要轮询这些更新的事件时,我发现最好按照 Google's Synchronize Resources 演练来获取 syncToken 并逐步获取事件。在他们的 java 示例中,他们如何获取、存储和刷新 syncToken 是有意义的。但是,我似乎找不到 node.js.

的任何类型的文档(或任何其他使用它们的人)

问题

如何获得初始 syncToken 以在日历上执行初始同步?要么我一直在搜索错误的关键字,要么 node.js 框架目前不支持该功能,这让我感到惊讶。

上下文

这是我解释为 "full initial sync." 的代码 这是列出的快速入门的延续 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);
  });
}

console.log(response); 语句 returns...

{ kind: 'calendar#events',
  etag: '"**redacted**"',
  summary: 'me@gmail.com',
  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] } ] }

应该在这里找到nextSyncToken吗?

您执行了初始完全同步(不需要 syncToken),作为该完全同步响应的一部分,您获得了所有当前日历项目,并且(如果结果是分页的,则在最后一页)您获得了名为 nextSyncToken 的字段。然后,您使用该 nextSyncToken 值进行下一次增量同步。

来自 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.

您必须存储同步令牌,直到您执行下一次增量同步并将该令牌作为增量同步请求的一部分提供。当您进行下一次增量同步时,它会为您提供一个新的 syncToken,您将再次存储并用于下一次增量同步。

syncToken 是 Google 知道自上次同步后向您发送哪些更改事件的方式。

我发现只有当查询参数'orderBy'被排除在请求之外时,响应才会包含'nextSyncToken'。

使用同步令牌时所有不受支持的查询参数都记录在 https://developers.google.com/calendar/v3/reference/events/list 标题 nextSyncToken 下。