本頁面說明如何使用 API 金鑰存取 Google Cloud API 和接受 API 金鑰的服務。
並非所有 Google Cloud API 都接受 API 金鑰來授權使用。請查看要使用的服務或 API 說明文件,判斷該服務或 API 是否接受 API 金鑰。
如要瞭解如何建立及管理 API 金鑰 (包括限制 API 金鑰),請參閱「管理 API 金鑰」一文。
如要瞭解如何在 Google 地圖平台中使用 API 金鑰,請參閱 Google 地圖平台說明文件。如要進一步瞭解 API Keys API,請參閱 API Keys API 說明文件。事前準備
Select the tab for how you plan to use the samples on this page:
C#
To use the .NET samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
C++
To use the C++ samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
-
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.
For more information, see Set up ADC for a local development environment in the Google Cloud authentication documentation.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
在 REST 中使用 API 金鑰
如要透過 REST API 呼叫加入 API 金鑰,請使用 x-goog-api-key
HTML 標頭,如以下範例所示:
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 金鑰,因此金鑰可能會在網址掃描中遭竊。
以下範例說明如何在 documents.analyzeEntities
的 Cloud Natural Language API 要求中使用 key
查詢參數。將 API_KEY
替換為 API 金鑰的金鑰字串。
POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY
在用戶端程式庫中使用 API 金鑰
本範例使用 Cloud Natural Language API (可接受 API 金鑰),說明如何為程式庫提供 API 金鑰。
C#
如要執行此範例,您必須安裝 Natural Language 用戶端程式庫。
C++
如要執行此範例,您必須安裝 Natural Language 用戶端程式庫。
Go
如要執行此範例,您必須安裝 Natural Language 用戶端程式庫。
Node.js
如要執行此範例,您必須安裝 Natural Language 用戶端程式庫。
Python
如要執行此範例,您必須安裝 Natural Language 用戶端程式庫。
在應用程式中使用 API 金鑰時,請確保在儲存和傳輸期間都能安全無虞。公開曝光 API 金鑰可能會導致您的帳戶產生意外的費用。詳情請參閱「API 金鑰的最佳管理做法」。
後續步驟
- 請參閱驗證方法總覽。
- 進一步瞭解 API Keys API。