リソースキーを使用してリンク共有ドライブのファイルにアクセスする
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
ドライブの UI または
Google Drive API を使用して、Google ドライブのファイルやフォルダを他のユーザーと共有できます。ドライブから共有する際は、ファイルの編集、ファイルへのコメント、ファイルの閲覧のみ、のいずれかの権限を共有相手に対して設定できます。
リソースキーは、意図しないアクセスからファイルを保護するのに役立ちます。リソースキーは、リンクを使用して共有された特定のファイルにユーザーがアクセスできるように渡される追加のパラメータです。そのファイルを閲覧したことのないユーザーがファイルにアクセスするには、リソースキーの入力が必要となります。最近そのファイルを閲覧したユーザーや、ファイルに直接アクセスできるユーザーは、リソースキーなしでファイルにアクセスできます。
リンクで共有されているドライブ ファイルは、type=user
または type=group
permissions
リソースの結果としてファイルにアクセスできるユーザーのみが検出できます。type=domain
または type=anyone
権限を使用してリンク共有ファイルにのみアクセスできるユーザーからのリクエストには、リソースキーが必要になる場合があります。
権限の詳細については、ファイル、フォルダ、ドライブを共有するをご覧ください。ロールと各ロールで許可されるオペレーションの一覧については、ロールと権限をご覧ください。
ファイルからリソースキーを読み取る
ドライブ API は、files
リソースの読み取り専用の resourceKey
フィールドでファイルのリソースキーを返します。
ファイルが ドライブ ショートカットの場合、ショートカット ターゲットのリソースキーは読み取り専用の shortcutDetails.targetResourceKey
フィールドで返されます。
files
リソースの exportLinks
、webContentLink
、webViewLink
などの URL を返すフィールドにも、resourceKey
が含まれます。ドライブ UI と統合するクライアントは、state
パラメータ内で resourceKeys
を使用することもできます。詳しくは、ファイルのダウンロードと書き出しをご覧ください。
リクエストにリソースキーを設定する
Drive API へのリクエストで参照されるファイルのすべてのリソースキーは、X-Goog-Drive-Resource-Keys
HTTP ヘッダーに設定されます。
Drive API へのリクエストでは、X-Goog-Drive-Resource-Keys
HTTP ヘッダーで 1 つ以上のリソースキーを指定できます。
構文
ファイル ID とリソースキーのペアは、スラッシュ(/
)区切り文字を使用してヘッダーに設定されます。ヘッダーは、カンマ(,
)区切り文字を使用してすべてのファイル ID とリソースキーのペアを組み合わせて作成されます。
たとえば、ファイル fileId1
をフォルダ fileId2
からフォルダ fileId3
に移動するリクエストを考えてみましょう。これらの 3 つのファイルのリソースキーは、それぞれ resourceKey1
、resourceKey2
、resourceKey3
であるとします。これらの値からスラッシュとカンマの区切り文字を使用して作成されたヘッダーは次のようになります。
X-Goog-Drive-Resource-Keys: fileId1/resourceKey1,fileId2/resourceKey2,fileId3/resourceKey3
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-29 UTC。
[null,null,["最終更新日 2025-08-29 UTC。"],[],[],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)"]]