Google 日历推送通知 404 错误

Google Calendar Push notification 404 error

我已经使用以下命令订阅了一个日历:

calendar.events
          .watch({
            calendarId: aCal.id,
            requestBody: {
              id: uuid.v1(),
              type: "web_hook",
              address: 'http://mywebsite.com',
            },
          })
          .catch((e) => e)

每次我创建活动时,我都会收到一条通知 "x-goog-resource-id":"unzJsxcVNifZI5ogOmIKRJ3Oiog"

这个ID是什么? Google 文档指出它是一个“不透明的资源 ID”,但是当我尝试获取具有此 ID 的日历时,我得到了 404。

此 ID 不是事件 ID,因为它对于我在单个日历中创建的每个事件都是相同的。当我在另一个日历中创建活动时,此 ID 会发生变化。所以它可能是一个日历 ID。

不幸的是,我每次得到以下请求:

calendar.events.list(
      {
        calendarId: calId,
      },
      (err, res) => {
        if (err) reject(err);
    
        resolve(res.data.items);
      }
    );

returns 我是 404。与 Google API 资源管理器相同。

这个ID是什么?我如何知道带有 Id 的日历中发生了什么变化?

感谢您的帮助,

通知不会告诉您什么发生了变化,只是告诉您已经发生了变化。

x-goog-resource-id代表的是具体的通知渠道,不是您正在观看的资源。

由于您可以在许多不同的资源(Acl、CalendarList、Events 和 Settings 资源)上设置通知,您只需跟踪通知渠道的 ID 及其对应的资源。通知还包含 x-goog-resource-uri,它应该指向已更改的资源。

设置频道

当您发出“watch”请求时。 IE。创建一个新的通知通道,到这个示例端点:

https://www.googleapis.com/calendar/v3/calendars/mygreatcalendar@group.calendar.google.com/events/watch

有了这个body:

{
  "id": "whateverIdyouFeltlikeAdding",
  "type": "web_hook",
  "address": "https://yourwebhookaddress.com"
}

你应该得到这样的回复(ID 是假的):

{
    "kind" : "api#channel",
    "expiration" : "1617113510000",
    "resourceId" : "2iPJp6kI2131231245543aQ_rIFGwE",
    "id" : "whateverIdyouFeltlikeAdding",
    "resourceUri" : "https://www.googleapis.com/calendar/v3/calendars/mygreatcalendar@group.calendar.google.com/events?alt=json"
}

那么你应该发出一个 GET 请求来拍摄当时日历的“快照”。

收到通知并找出发生了什么变化

当您更改事件时,您应该会收到包含此信息的通知(此示例已汇总并包含虚假数据):

'x-goog-channel-id': 'whateverIdyouFeltlikeAdding',
'x-goog-message-number': '3313856',
'x-goog-resource-id': '2iPJp6kI2131231245543aQ_rIFGwE',
'x-goog-resource-state': 'exists',
'x-goog-resource-uri': 'https://www.googleapis.com/calendar/v3/calendars/mygreatcalendar@group.calendar.google.com/events?alt=json',

Note that the resourceID from the creation of the channel matches the x-goog-resource-id (even though these particular IDs are fake). The x-goog-channel-id also matches the custom id you give on its creation.

要识别已更改的资源,您需要从创建频道时存储这些 ID,您可以使用 x-goog-channel-idx-goog-resource-id 将它们与本地存储进行比较。

您的本地商店可能很简单 JSON:

{
    '2iPJp6kI2131231245543aQ_rIFGwE' : 'mygreatcalendar@group.calendar.google.com'
}

或者您可以只解析 x-goog-resource-uri 以获取日历 ID。

一旦您确定了资源,您将不得不发出另一个 GET 请求来与您的“快照”进行比较。您可以使用响应中包含的 etag 来快速扫描哪些特定资源已更改。

文档