Google 翻译 API 密钥 json 格式

Google Translate API Key json format

我正在尝试使用来自本地计算机(而非 google 云)的 Google 翻译 API。

在 运行 之前我有 运行

export GOOGLE_APPLICATION_CREDENTIALS=API.json

其中 API.json 是:

{ "key":"the_key_code" }

运行使用此代码后:

import google.auth
from google.cloud import translate
_, PROJECT_ID = google.auth.default()
TRANSLATE = translate.TranslationServiceClient()

我收到以下错误:

DefaultCredentialsError: The file API_.json does not have a valid type. Type is None, expected one of ('authorized_user', 'service_account', 'external_account')

然后我将类型:service_account 添加到 json 并得到了这个错误:

DefaultCredentialsError: ('Failed to load service account credentials from API.json', ValueError('Service account info was not in the expected format, missing fields client_email, token_uri.'))

我找不到任何关于 API 密钥 json 文件格式的文档,也无法从 [=47= 导出 json 文件] 云平台,只有复制密钥的能力。

关于如何从本地机器翻译工作有什么想法吗?

我也必须为 Analytics API 创建一个凭据文件,并且同样面临信息不足的问题。 我没办法,用了新的googleCloud Dashboard,你得自己创建一个应用,select你要的API,然后Google给你一个有效的凭据文件。

我的文件看起来像这样:

{
  "type": "service_account",
  "project_id": "***",
  "private_key_id": "***",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB***xmw0Fcs=\n-----END PRIVATE KEY-----\n",
  "client_email": "***",
  "client_id": "***",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "***"
}

这就是为什么我很确定你必须对翻译做同样的事情 API,第一个声明 "type": "service_account", 与你得到的错误相匹配。