本页面介绍了如何创建、修改和限制 API 密钥。 如需了解如何使用 API 密钥访问 Google API,请参阅使用 API 密钥访问 API。
API 密钥简介
API 密钥有两种类型:标准 API 密钥和绑定到服务账号的 API 密钥。
标准 API 密钥
标准 API 密钥提供了一种方法,可将请求与项目相关联,以进行结算和配额计算。 使用标准 API 密钥(未绑定到服务账号的 API 密钥)访问 API 时,API 密钥不会标识主账号。如果没有主账号,请求就无法使用 Identity and Access Management (IAM) 检查调用方是否有权执行请求的操作。
标准 API 密钥可与任何接受 API 密钥的 API 搭配使用,除非已向密钥添加 API 限制。标准 API 密钥无法与不接受 API 密钥的服务搭配使用,包括在快速模式下。
绑定到服务账号的 API 密钥
绑定到服务账号的 API 密钥可为请求提供服务账号的身份和授权。当您使用绑定到服务账号的 API 密钥来访问 API 时,系统会将您的请求视为使用绑定的服务账号发出请求来进行处理。
唯一支持绑定 API 密钥的 API 是 aiplatform.googleapis.com。
默认组织政策限制条件会阻止将密钥绑定到服务账号。如需更改此设置,请参阅启用将密钥绑定到服务账号功能。
API 密钥组成部分
API 密钥包含以下组成部分,可用于管理和使用密钥:
- 字符串
- API 密钥字符串是加密的字符串,例如
AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe。当您使用 API 密钥访问 API 时,始终是使用密钥的字符串。API 密钥没有关联的 JSON 文件。 - ID
- Google Cloud 管理工具使用 API 密钥 ID 来唯一标识密钥。密钥 ID 无法用于访问 API。您可以在 Google Cloud 控制台的密钥修改页面的网址中找到密钥 ID。您还可以使用 Google Cloud CLI 列出项目中的密钥来获取密钥 ID。
- 显示名称
- 显示名称是密钥的可选描述性名称,您可以在创建或更新密钥时设置该名称。
- 已绑定的服务账号
- 绑定到服务账号的 API 密钥包含服务账号的邮箱。
准备工作
如需使用本页面上的示例,请完成以下任务。
设置身份验证
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
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.
-
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。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 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.
-
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
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.
-
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
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 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.
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获得管理 API 密钥所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
-
API Keys Admin (
roles/serviceusage.apiKeysAdmin) -
使用 Google Cloud 控制台将 API 密钥限制为特定 API:Service Usage Viewer (
roles/serviceusage.serviceUsageViewer)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
启用将密钥绑定到服务账号功能
您需要先将 constraints/iam.managed.disableServiceAccountApiKeyCreation 组织政策限制条件设置为 false,然后才能将 API 密钥绑定到服务账号。更改组织政策需要关联的组织资源。不支持没有组织的项目。
如需更改政策限制条件,请按照以下说明操作。
控制台
在 Google Cloud 控制台中,转到组织政策页面。
切换到您要更改其政策的组织、文件夹或项目。
在过滤条件框中,输入
Block service,然后点击过滤条件名称 Block service account API key bindings。点击管理政策。
在政策来源部分中,选择覆盖父资源的政策。
点击添加规则,然后将强制执行设置为关闭。
点击完成。
可选:点击测试更改,以便深入了解所建议的政策可能会如何违反或破坏合规性。
点击设置政策。
gcloud
创建一个名为
spec.yaml的文件,其中包含以下内容:name: SCOPE/SCOPE_ID/policies/iam.managed.disableServiceAccountApiKeyCreation spec: rules: - enforce: false请提供以下值:
SCOPE:organizations、folders或projects。