如何在 Google Drive API Node.js SDK 中获取具有资源 ID 的资源?

How can I get the resource with a resource ID in Google Drive API Node.js SDK?

原问题

我为 Google 驱动器文件夹注册了一个监视频道,并收到了它已更新的通知。 (使用 Google 驱动器 API Node.js SDK v3)

{
  host: 'my-development-host.example.com',
  'user-agent': 'APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)',
  'content-length': '0',
  accept: '*/*',
  'accept-encoding': 'gzip,deflate,br',
  'x-forwarded-for': 'xxx.xxx.xxx.xxx',
  'x-forwarded-proto': 'https',
  'x-goog-changed': 'children',
  'x-goog-channel-expiration': 'Tue, 11 May 2021 10:21:53 GMT',
  'x-goog-channel-id': '8f76c160-b229-11eb-a810-93ba607a525b',
  'x-goog-message-number': '1356107',
  'x-goog-resource-id': 'li76v1_bIpI23HBJ13dAqo66pYQ',
  'x-goog-resource-state': 'update',
  'x-goog-resource-uri': 'https://www.googleapis.com/drive/v3/files/1_ihmP2rHCBMXe7aQkW32bjZMccXzsPjE?acknowledgeAbuse=false&supportsAllDrives=false&supportsTeamDrives=false&alt=json'
}

我正在查看的文件夹的文件 ID 是 1_ihmP2rHCBMXe7aQkW32bjZMccXzsPjE,我知道它包含在 x-goog-resource-uri 中,因此我可以访问具有该文件 ID 的文件夹。

the official document 中,资源 ID 与版本无关。

Note: The resourceId property is a stable, version-independent identifier for the resource. The resourceUri property is the canonical URI of the watched resource in the context of the current API version, so it’s version-specific.

我想获取与版本无关的资源数据。 但是,我不知道如何获取资源数据x-goog-resource-id,资源ID。

请问有谁能告诉我吗?如果可能的话,不是 Node.js SDK,而是 API 风格,就可以了。

编辑

根据官方文档中的the sampleresourceId与资源URI中的相同,注释说是ID of the watched resource。我认为watched资源是指文件资源。

下面的示例是从中复制的:

{
  "kind": "api#channel",
  "id": "01234567-89ab-cdef-0123456789ab", // ID you specified for this channel.
  "resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
  "resourceUri": "https://www.googleapis.com/drive/v3/files/o3hgv1538sdjfh", // Version-specific ID of the watched resource.
  "token": "target=myApp-myFilesChannelDest", // Present only if one was provided.
  "expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}

在我的例子中,我收到了类似的响应,但 URI 样式和资源 ID 用法(?)可能与样本不同。

{
  kind: 'api#channel',
  id: '8f76c160-b229-11eb-a810-93ba607a525b',
  resourceId: 'li76v1_bIpI23HBJ13dAqo66pYQ',
  resourceUri: 'https://www.googleapis.com/drive/v3/files/1_ihmP2rHCBMXe7aQkW32bjZMccXzsPjE?acknowledgeAbuse=false&supportsAllDrives=false&supportsTeamDrives=false&alt=json',
  expiration: '1620728513000'
}

x-goog-resource-id 与您正在观看的 file/folder ID 不同。

来自文档:

An opaque value that identifies the watched resource. This ID is stable across API versions.

Understanding the notification message format

这意味着 Google 将为 file/folder 独有的监视资源创建一个新 ID,但与 file/folder ID 不同。不透明的部分意味着这只是您识别观看频道的另一个ID。 Google 可能在内部使用它,但不透明意味着我们看不到它的工作原理。

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

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

{
    '2iPJp6kI2131231245543aQ_rIFGwE' : '[FILE_ID]'
}

或者您可以只解析 x-goog-resource-uri 以获取 ID,就像您已经完成的那样。

要随后获取有关修订和 version-dependent/independent 数据的更多数据,您只需在收到推送通知后对驱动器 API 使用普通 GET 请求。

您链接的示例

这显示了 watch 请求响应:

{
  "kind": "api#channel",
  "id": "01234567-89ab-cdef-0123456789ab"", // ID you specified for this channel.
  "resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
  "resourceUri": "https://www.googleapis.com/drive/v3/files/o3hgv1538sdjfh", // Version-specific ID of the watched resource.
  "token": "target=myApp-myFilesChannelDest", // Present only if one was provided.
  "expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}

这是您在创建通知渠道时获得的一次性响应。这不是您在资源更改时收到的推送通知的格式。

文档