使用资源密钥访问链接共享的云端硬盘文件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用
Drive 界面或通过
Google Drive API 与他人共享 Google 云端硬盘文件和文件夹。通过云端硬盘共享文件时,您可以控制用户是能够编辑文件或评论文件,还是只能打开文件。
资源密钥有助于保护文件免遭意外访问。资源密钥是传递的附加参数,可让用户访问通过链接共享的某些文件。之前未查看过该文件的用户必须提供资源密钥才能获得访问权限。最近查看过该文件或拥有直接访问权限的用户不需要使用资源密钥即可访问该文件。
通过链接共享的云端硬盘文件只能由以下用户发现:因 type=user
或 type=group
permissions
资源而可以访问该文件的用户。如果用户仅有权访问通过链接共享的文件,且使用的是 type=domain
或 type=anyone
权限,则可能需要资源密钥。
如需详细了解权限,请参阅共享文件、文件夹和云端硬盘。如需查看角色和每种角色允许的操作的完整列表,请参阅角色和权限。
从文件中读取资源密钥
Drive API 会在 files
资源的只读 resourceKey
字段中返回文件的资源密钥。
如果文件是云端硬盘快捷方式,则快捷方式目标的资源键会返回到只读 shortcutDetails.targetResourceKey
字段中。
返回网址的 files
资源中的字段(例如 exportLinks
、webContentLink
和 webViewLink
)也包含 resourceKey
。与云端硬盘界面集成的客户端也可以在 state
参数中使用 resourceKeys
。如需了解详情,请参阅下载和导出文件。
在请求中设置资源键
对 Drive API 的请求所引用的任何文件的资源密钥都设置在 X-Goog-Drive-Resource-Keys
HTTP 标头上。
对 Drive API 的请求可以使用 X-Goog-Drive-Resource-Keys
HTTP 标头指定一个或多个资源键。
语法
文件 ID 和资源键对通过正斜线 (/
) 分隔符设置在标头中。通过使用英文逗号 (,
) 分隔符组合所有文件 ID 和资源键对来构建标头。
例如,假设有一项请求是将文件 fileId1
从文件夹 fileId2
移动到文件夹 fileId3
。假设这三个文件的资源键分别为 resourceKey1
、resourceKey2
和 resourceKey3
。使用正斜杠和英文逗号分隔符根据这些值构建的标头如下所示:
X-Goog-Drive-Resource-Keys: fileId1/resourceKey1,fileId2/resourceKey2,fileId3/resourceKey3
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Access link-shared Drive files using resource keys\n\nYou can share Google Drive files and folders with others using the [Drive UI](/workspace/drive/api/guides/about-apps) or through the [Google Drive API](/workspace/drive/api/guides/create-file). When you share from\nDrive, you can control whether people can edit, comment on, or\nonly open the file.\n\nA *resource key* helps protect your file from unintended access. Resource keys\nare an additional parameter that are passed so users can access certain files\nthat have been shared using a link. Users who haven't viewed the file before\nmust provide the resource key to gain access. Those who have recently viewed the\nfile, or have direct access, don't need the resource key to access the file.\n\nA Drive file that's shared with a link can only be discovered by\nusers that can access the file as a result of a `type=user` or `type=group`\n[`permissions`](/workspace/drive/api/reference/rest/v3/permissions) resource. Requests from users\nthat only have access to these link-shared files using a `type=domain` or\n`type=anyone` permission might require a resource key.\n\nFor more information about permissions, see [Share files, folders and drives](/workspace/drive/api/guides/manage-sharing). For a complete list of roles and the operations\npermitted by each, see [Roles \\& permissions](/workspace/drive/api/guides/ref-roles).\n\nRead the resource key from the file\n-----------------------------------\n\nThe Drive API returns a file's resource key on the read-only\n`resourceKey` field of the [`files`](/workspace/drive/api/reference/rest/v3/files) resource.\n\nIf the file is a [Drive shortcut](/workspace/drive/api/guides/shortcuts), the\nresource key for the shortcut target is returned on the read-only\n`shortcutDetails.targetResourceKey` field.\n\nFields in the [`files`](/workspace/drive/api/reference/rest/v3/files) resource that return URLs,\nsuch as `exportLinks`, `webContentLink`, and `webViewLink`, also include the\n`resourceKey`. Clients that integrate with the Drive UI can also\nuse `resourceKeys` within the [`state`](/workspace/drive/api/guides/enable-sdk#construct)\nparameter. For more information, see [Download and export\nfiles](/workspace/drive/api/guides/manage-downloads).\n\nSet the resource key on the request\n-----------------------------------\n\nResource keys for any files referenced by requests to the Drive API\nare set on the `X-Goog-Drive-Resource-Keys` HTTP header.\n\nRequests to the Drive API can specify one or more resource keys with\nthe `X-Goog-Drive-Resource-Keys` HTTP header.\n\n### Syntax\n\nA file ID and resource key pair are set on the header using a forward slash\n(`/`) separator. The header is built by combining all the file ID and resource\nkey pairs using comma (`,`) separators.\n\nFor example, consider a request to move file `fileId1` from folder `fileId2` to\nfolder `fileId3`. Assume the resource keys for these three files are\n`resourceKey1`, `resourceKey2`, and `resourceKey3`, respectively. The header\nbuilt from these values using a forward slash and comma separators is: \n\n X-Goog-Drive-Resource-Keys: fileId1/resourceKey1,fileId2/resourceKey2,fileId3/resourceKey3\n\n### Related topics\n\n- [Share files, folders and drives](/workspace/drive/api/guides/manage-sharing)\n- [Download and export files](/workspace/drive/api/guides/manage-downloads)\n- [Protect file content](/workspace/drive/api/guides/content-restrictions)\n- [Configure a Drive UI integration](/workspace/drive/api/guides/enable-sdk)"]]