为本地开发环境设置 ADC (original) (raw)

您可以在本地开发环境中向 ADC 提供用户凭证服务账号凭证

用户凭据

当您的代码在本地开发环境(例如开发工作站)中运行时,最好的方法是使用与您的用户账号关联的凭据。

使用用户账号配置 ADC 的方式取决于用户账号是由 Google 管理(换句话说,它是 Google 账号)还是由其他身份提供方 (IdP) 管理,并且用户账号通过使用员工身份联合进行联合。

Google 账号

如需使用 Google 账号配置 ADC,您可以使用 Google Cloud CLI:

  1. 安装 Google Cloud CLI。
    如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI
  2. 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.
    登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。

外部 IdP

如需为由外部 IdP 管理并使用员工身份联合进行联合的用户账号配置 ADC,请执行以下操作:

  1. 安装 Google Cloud CLI。 安装完成后,运行以下命令来初始化 Google Cloud CLI:
    gcloud init
    如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI
  2. 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.
    登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。

使用用户凭据配置 ADC 的提示

使用用户账号配置 ADC 时,您应该注意以下事项:

服务账号凭证

您可以使用服务账号模拟或服务账号密钥,通过服务账号的凭证来配置 ADC。

服务账号模拟

您可以使用服务账号模拟来设置本地应用默认凭据 (ADC) 文件。支持模拟的客户端库可以自动使用这些凭据。使用模拟创建的本地 ADC 文件支持以下语言:

您必须具有要模拟的服务账号的 Service Account Token Creator (roles/iam.serviceAccountTokenCreator) IAM 角色。如需了解详情,请参阅必需的角色

使用服务账号模拟创建本地 ADC 文件:

gcloud auth application-default login --impersonate-service-account SERVICE_ACCT_EMAIL

现在,您可以通过支持的语言使用客户端库,就像使用用户凭据设置本地 ADC 文件一样。身份验证库会自动查找凭据。如需了解详情,请参阅使用客户端库时进行身份验证

并非所有身份验证库都支持使用服务账号模拟生成的本地 ADC 文件中的凭据。如需了解详情,请参阅服务账号模拟返回的本地凭据错误

服务账号密钥

如果您无法使用用户账号或服务账号模拟进行本地开发,可以使用服务账号密钥。

如需创建服务账号密钥并将其提供给 ADC,请执行以下操作:

  1. 按照创建服务账号密钥中的说明,创建一个具有您的应用所需的角色的服务账号,并为该服务账号创建密钥。
  2. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
    Example: Linux or macOS
    export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
    Replace KEY_PATH with the path of the JSON file that contains your credentials.
    For example:
    export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
    Example: Windows
    For PowerShell:
    $env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
    Replace KEY_PATH with the path of the JSON file that contains your credentials.
    For example:
    $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
    For command prompt:
    set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
    Replace KEY_PATH with the path of the JSON file that contains your credentials.

后续步骤