e.i*_*luf 2 google-api node.js google-text-to-speech service-accounts google-cloud-platform
我正在尝试在我的 Node.js 中使用 google 文本转语音和其他翻译服务,但是当我连接到 google api 时,我收到此错误消息
“您的应用程序已使用来自 Google Cloud SDK 或 Google Cloud Shell 的最终用户凭据进行身份验证,但 texttospeech.googleapis.com 不支持这些凭据。我们建议在 gcloud 中配置 billing/quota_project 设置,或通过 auth/impersonate_service_account 使用服务帐户设置。有关服务帐户以及如何在应用程序中使用它们的更多信息,请参阅https://cloud.google.com/docs/authentication/。如果您使用curl或类似工具遇到此错误,您可能需要指定“X-Goog-User-Project”HTTP 标头用于配额和计费目的。有关“X-Goog-User-Project”标头的更多信息,请查看https://cloud.google.com/apis/docs/system-参数。” ,元数据:元数据{internalRepr:Map(2){'google.rpc.errorinfo-bin'=> [Array],'grpc-status-details-bin'=> [Array]},选项:{}},注意: '重试方法中发生异常,未归类为瞬态'}
因此,经过多次研究,我尝试验证我是否正在使用我的服务帐户凭据进行身份验证。我运行了这个命令
gcloud auth activate-service-account --key-file=./auth/service_acct_key.json
Run Code Online (Sandbox Code Playgroud)
它表明了这一点
已激活的服务帐户凭据:[firebase-adminsdk-uwecx@xxxxx.iam.gserviceaccount.com]
但是当再次运行服务器时
node server.js
Run Code Online (Sandbox Code Playgroud)
我仍然收到错误
是什么导致了此错误以及如何正确进行身份验证?
使用 gcloud CLI,您有 2 级身份验证:
当您执行该命令时,gcloud auth ....您处于 CLI 级别
当您执行该命令时,gcloud auth application-default ...您处于 ADC 级别。
在您的情况下,您仅在 CLI 级别设置身份验证,当然,在您的 NODE 应用程序中不会检测到该身份验证,该应用程序在 ADC 级别使用 Google Cloud 库和搜索凭据。
当您使用服务帐户密钥文件时(这是一种不好的做法,但在教程中经常出现和共享,甚至在 Google Cloud 教程中也是如此(...)),您必须设置一个环境变量GOOGLE_APPLICATION_CREDENTIALS,其值等于绝对路径您的服务帐户密钥文件。尝试一下
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/auth/service_acct_key.json
node server.js
Run Code Online (Sandbox Code Playgroud)
它应该有效。
| 归档时间: |
|
| 查看次数: |
870 次 |
| 最近记录: |