本頁說明如何使用 API 金鑰存取接受 API 金鑰的 Google Cloud API 和服務。
並非所有 Google Cloud API 都接受 API 金鑰授權使用。請參閱要使用的服務或 API 說明文件,確認是否接受 API 金鑰。
如要瞭解如何建立及管理 API 金鑰 (包括限制 API 金鑰),請參閱「管理 API 金鑰」一文。
如要瞭解如何搭配使用 API 金鑰與 Google 地圖平台,請參閱 Google 地圖平台說明文件。 如要進一步瞭解 API 金鑰 API,請參閱 API 金鑰 API 說明文件。事前準備
Select the tab for how you plan to use the samples on this page:
C#
如要在本機開發環境中使用本頁的 .NET 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
C++
如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
搭配 REST 使用 API 金鑰
如要在 REST API 呼叫中加入 API 金鑰,請使用 x-goog-api-key
HTTP 標頭,如下列範例所示:
curl -X POST \ -H "X-goog-api-key: API_KEY" \ -H "Content-Type: application/json; charset=utf-8" \ -d @request.json \ "https://translation.googleapis.com/language/translate/v2"
如果無法使用 HTTP 標頭,可以改用 key
查詢參數。不過,這個方法會在網址中加入 API 金鑰,導致金鑰暴露在網址掃描中,容易遭到竊取。
下列範例說明如何搭配 Cloud Natural Language API 要求使用 key
查詢參數,以取得 documents.analyzeEntities
。將 API_KEY
替換成 API 金鑰的金鑰字串。
POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY
搭配用戶端程式庫使用 API 金鑰
本範例使用接受 API 金鑰的 Cloud Natural Language API,示範如何向程式庫提供 API 金鑰。
C#
如要執行這個範例,您必須安裝 Natural Language 用戶端程式庫。
C++
如要執行這個範例,您必須安裝 Natural Language 用戶端程式庫。
Go
如要執行這個範例,您必須安裝 Natural Language 用戶端程式庫。
Node.js
如要執行這個範例,您必須安裝 Natural Language 用戶端程式庫。
Python
如要執行這個範例,您必須安裝 Natural Language 用戶端程式庫。
在應用程式中使用 API 金鑰時,請確保儲存和傳輸期間的金鑰安全無虞。公開 API 金鑰可能會導致帳戶產生非預期費用。詳情請參閱「管理 API 金鑰的最佳做法」。
後續步驟
- 請參閱驗證方法總覽。
- 進一步瞭解 API 金鑰 API。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-15 (世界標準時間)。