如何使用服务帐户向 google 文本转语音进行身份验证

how to authenticate to google text-to-speech with service account

我正在尝试在我的 nodejs 中使用 google 文本到语音和其他 t运行slation 服务但是当我连接到 google api 我得到此错误消息

"Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the texttospeech.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/. If you are getting this error with curl or similar tools, you may need to specify 'X-Goog-User-Project' HTTP header for quota and billing purposes. For more information regarding 'X-Goog-User-Project' header, please check https://cloud.google.com/apis/docs/system-parameters.", metadata: Metadata { internalRepr: Map(2) { 'google.rpc.errorinfo-bin' => [Array], 'grpc-status-details-bin' => [Array] }, options: {} }, note: 'Exception occurred in retry method that was not classified as transient' }

因此,经过多次研究后,我尝试验证我是否正在使用我的服务帐户凭据进行身份验证。我运行这个命令

gcloud auth activate-service-account --key-file=./auth/service_acct_key.json

它显示了这个

已激活服务帐户凭据:[firebase-adminsdk-uwecx@xxxxx.iam.gserviceaccount.com]

但是当运行服务器再次

node server.js 

我仍然遇到错误

导致此错误的原因是什么以及如何正确验证?

使用 gcloud CLI,您有 2 级身份验证:

  • CLI 级别
  • Google Cloud Auth 库级别(也称为 ADC,用于应用程序默认凭据)

当您执行命令时 gcloud auth .... 您处于 CLI 级别

当您执行命令时gcloud auth application-default ...您处于ADC级别。


在你的情况下,你只在 CLI 级别设置身份验证,当然,在你的 NODE 应用程序中没有检测到身份验证,它使用 Google 云库和 ADC 级别的搜索凭证.

当您使用服务帐户密钥文件时(这是一种不好的做法,但在教程中甚至在 Google 云教程中也经常出现和共享 (...)),您必须设置一个环境变量 GOOGLE_APPLICATION_CREDENTIALS,其值等于您的服务帐户密钥文件的绝对路径。试试看

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/auth/service_acct_key.json
node server.js

应该可以。