本頁面說明如何使用 API 金鑰方法執行下列操作:
事前準備
這個頁面會使用 curl 和 Google Cloud CLI,將要求傳送至 API 金鑰 API。如要瞭解如何設定 API 實驗環境,請參閱「開始使用 API 金鑰」一文。
列出專案中的 API 金鑰
您可以使用 Google Cloud ListKeys 方法,取得專案擁有的所有金鑰清單:
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 金鑰的清單。如要查看所有金鑰的清單 (包括標示為要刪除的金鑰),請新增查詢參數 show_deleted=true:
gcurl https://apikeys.googleapis.com/v2/projects/PROJECT_NUMBER/locations/global/keys?show_deleted=true
成功時,您會收到類似以下的回應:
{
"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=="
},
{
"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=="
}
]
}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
取得金鑰字串
為確保安全性,ListKeys 和 GetKey 方法不會傳回 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 金鑰建立所在 Google Cloud 專案的專案編號。name欄位包含 API 金鑰的專屬 ID。