取得 API 金鑰的相關資訊

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

本頁面說明如何使用 API 金鑰方法執行下列操作:

事前準備

頁面使用 curl 搭配 oauth2l 指令列工具,將要求傳送至 API 金鑰的 API。如要進一步瞭解如何設定 API 實驗,請參閱開始使用 API 金鑰

列出專案中的 API 金鑰

您可以使用 ListKeys 方法取得 Google Cloud 專案擁有的所有金鑰清單:

gcurl https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys

成功之後,您會看到類似以下的回應:

{
  "keys": [
    {
      "name": "projects/12345678/locations/global/keys/2885bf87-5b84-47fa-92af-08c3e9337349",
      "displayName": "API key 2",
      "createTime": "2019-05-29T22:07:22.036Z",
      "uid": "2885bf87-5b84-47fa-92af-08c3e9337349",
      "updateTime": "2019-05-29T22:07:22.058623Z",
      "restrictions": {
        "androidKeyRestrictions": {}
      },
      "etag": "zHib8eXEMCxe4ayQEbIaZg=="
    },
    {
      "name": "projects/12345678/locations/global/keys/a4db08b7-5729-4ba9-8c08-f2df493465a1",
      "displayName": "API key 1",
      "createTime": "2019-05-29T22:06:58.844Z",
      "uid": "a4db08b7-5729-4ba9-8c08-f2df493465a1",
      "updateTime": "2019-05-29T22:06:58.855103Z",
      "restrictions": {
        "androidKeyRestrictions": {}
      },
      "etag": "0L5KcPMGoNi53K5+FqPxiw=="
    }
  ]
}

根據預設,ListKeys 方法會傳回可用的 API 金鑰清單。如要查看標記為要刪除的清單鍵,請使用 filter=state:DELETED 新增查詢參數:

gcurl https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys?filter=state:DELETED

成功之後,您會看到類似以下的回應:

{
  "keys": [
    {
      "name": "projects/12345678/locations/global/keys/5d3564ad-f08e-48df-b0ca-0f50858ba3f2",
      "displayName": "Key 1",
      "createTime": "2019-06-12T04:47:30.214Z",
      "uid": "5d3564ad-f08e-48df-b0ca-0f50858ba3f2",
      "updateTime": "2021-03-05T22:35:37.387645Z",
      "deleteTime": "2021-03-05T22:35:37.290544Z",
      "etag": "V96UGAyyz+6sUHttzK42pQ=="
    },
    {
      "name": "projects/12345678/locations/global/keys/7ad567fa-c11b-4903-99dc-88f89da7d73a",
      "displayName": "Key 2",
      "createTime": "2019-06-12T00:47:27.778Z",
      "uid": "7ad567fa-c11b-4903-99dc-88f89da7d73a",
      "updateTime": "2021-03-05T22:23:57.002187Z",
      "deleteTime": "2021-03-05T22:23:56.350234Z",
      "etag": "njPE6YjwHlrh6TLyxHibBg=="
    }
  ]
}

發出刪除指令時,deleteTime 欄位會儲存。可在刪除指令後 30 天永久刪除金鑰。

如果專案中有多個 API 金鑰,您可能會希望將結果設為網頁,而非傳回所有金鑰。如要控管每頁的數量或結果,請在您要傳回的結果數量中加入 pageSize 查詢參數:

gcurl https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys?pageSize=25

回應結尾包含 nextPageToken 欄位。如要取得下一個結果頁面,請加入 pageToken 查詢參數,以在下一個呼叫中加入憑證:

gcurl 'https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys?pageSize=25&pageToken=TOKEN'

請注意,由於 & 是特殊的 Bash 字元,因此您必須以單引號括住網址。

取得 API 金鑰的中繼資料

如要取得特定 API 金鑰的中繼資料,請使用 Key.name 呼叫 GetKey 方法:

gcurl https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys/KEY_ID

取得金鑰字串

為了安全起見,ListKeysGetKey 方法不會傳回 Key.keyString。如要取得 API 金鑰的 keyString,請使用 Key.name 呼叫 GetKeyString 方法:

gcurl https://apikeys.googleapis.com/v2/Key.name/keyString

呼叫使用者必須擁有 apikeys.keys.getKeyString 權限才能呼叫 GetKeyString 方法。

依金鑰字串查詢金鑰名稱和專案

如果您擁有 keyString,而且需要查詢金鑰 name 和擁有 API 金鑰的專案,請呼叫 LookupKey 方法,並指定 keyString 查詢參數:

gcurl https://apikeys.googleapis.com/v2/keys:lookupKey?keyString=KEY_STRING

回應會類似以下內容:

{
  "parent": "projects/12345678/locations/global",
  "name": "projects/12345678/locations/global/keys/2c437be3-1c77-417b-8f6f-748189760227"
}

在回應中:

  • parent 欄位中包含建立 API 金鑰的 Cloud 專案編號。
  • name 欄位包含 API 金鑰的唯一識別碼。

後續步驟