如何获取 Google Translate API 的文件“service_account.json”?

Ala*_*xus 18 google-translate google-cloud-platform

我尝试在我的开发中使用 Google Translate API,但我找不到获取“service_account.json”文件的方法。

“Console Google Cloud Platform”的步骤:

控制台上的步骤:

在此处输入图片说明

拜托,我需要详细的步骤,因为我从谷歌得到的东西对我不起作用

d1s*_*ack 63

我花了一段时间才找到如何下载 json 密钥。

  1. 创建服务帐户
  2. 在云控制台中打开服务帐户并添加密钥 在此输入图像描述
  3. 在下拉菜单中选择创建密钥 4.然后选择密钥类型 json 在此输入图像描述


HJE*_*JED 27

  1. 转到https://console.cloud.google.com/apis/credentials
  2. 在左上角有一个蓝色的“创建凭据”按钮,单击它并选择“服务帐户密钥”。(如果不存在,请参见下文)
  3. 选择所需的服务帐户,然后选择“JSON”作为密钥类型。
  4. 它应该允许给你一个 json 下载

如果蓝色按钮不存在:

您需要在上面链接的页面上的“OAuth 同意屏幕”选项卡上填写所有必填字段,如果不存在则创建一个。

如果仍然不起作用,您可能还需要创建一个客户端 ID(抱歉,我不记得了)。

如果“服务帐户密钥”不是一个选项

您需要创建一个服务帐户。

转到https://console.cloud.google.com/iam-admin/serviceaccounts/project并单击“创建服务帐户”

  • 出于安全原因,Google 不会让您重新下载它(他们不存储私钥部分) (18认同)
  • 如果我已经创建了服务帐户密钥,但现在想再次下载它并且不想创建另一个密钥,该怎么办? (3认同)
  • 所以谷歌在我电脑的某个地方下载了密钥而没有问我在哪里?我不明白。 (3认同)

小智 17

获取 credential.js 的最新方法是单击“your_email_servicer”-> 选择 Tab 键,然后单击 [添加密钥]

在此输入图像描述

  • 这是创建一个新密钥,但如果我需要现有服务帐户的凭据怎么办 (3认同)

小智 6

服务帐户密钥只能在第一次创建 sa 时检索,特别是在您通过 GCP 控制台执行此操作的情况下,这是一种安全机制。但这没什么大不了的,你可以删除旧的并创建一个新的,就这样,SA仍然保持不变,轮换密钥很好。

如果您使用 GDM(Google 部署管理器)或 Terraform 等 IaC,您可以根据需要多次检索它。

对于 Terraform,只需配置必要的输出,如下所示......

### Resources

resource "google_service_account" "my_service_account_name" {
  account_id    = "my-sa"
  display_name  = "my-sa"
  description   = "This is for descriptions"
}

resource "google_service_account_key" "my_service_account_key" {
  service_account_id = google_service_account.my_service_account_name.name
}

### Outputs

output "my_service_account_key" {
  description = "This is for descriptions (for single use)."
  # sensitive   = true # you can add sensitive to avoid showing it in plain text each time you run the code in case you don't need to retrieve it each time
  value = base64decode(google_service_account_key.my_service_account_key.private_key)
}
Run Code Online (Sandbox Code Playgroud)

对于 GDM,请记住 GCP 不再建议将其用于 IaC 管理,您仍然可以使用描述资源的 CLI 来检索它并为 sa 关键资源添加基本的 jq 查询...

gcloud deployment-manager deployments describe my-gdm-deployment-name \
    --project project-name \
    --format json | jq -r '.outputs[] | select(.name==my-sa-key-resource-name") | .finalValue'| base64 --decode
Run Code Online (Sandbox Code Playgroud)