本页面介绍了如何使用 API 密钥访问 Google Cloud API 和接受 API 密钥的服务。
并非所有 Google Cloud API 都接受 API 密钥来授权使用。查看您要使用的服务或 API 的文档,以确定它是否接手 API 密钥。
如需了解如何创建和管理 API 密钥(包括限制 API 密钥),请参阅管理 API 密钥。
如需了解如何将 API 密钥与 Google Maps Platform 搭配使用,请参阅 Google Maps Platform 文档。如需详细了解 API Keys API,请参阅 API Keys API 文档。准备工作
Select the tab for how you plan to use the samples on this page:
C#
如需在本地开发环境中使用本页面上的 .NET 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
- Install the Google Cloud CLI.
-
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.
Google Cloud
C++
如需在本地开发环境中使用本页面上的 C++ 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
- Install the Google Cloud CLI.
-
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.
如需了解详情,请参阅 Google Cloud 身份验证文档中的为本地开发环境设置 ADC。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
- Install the Google Cloud CLI.
-
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.
Google Cloud
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
- Install the Google Cloud CLI.
-
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.
Google Cloud
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
- Install the Google Cloud CLI.
-
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.
Google Cloud
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
将 API 密钥与 REST 搭配使用
如需在 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"
如果您无法使用 HTML 标头,可以使用 key
查询参数。不过,此方法会在网址中包含您的 API 密钥,从而使您的密钥容易遭到网址扫描的窃取。
以下示例展示了如何将 key
查询参数与针对 documents.analyzeEntities
的 Cloud Natural Language API 请求搭配使用。将 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。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-02-16。