I am trying to use google text-to-speech and other translation service in my nodejs but when i connect to google api I get this error message
"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' }
so after many research i tried to verify that i am authenticating using my service account credentials. I ran this command
gcloud auth activate-service-account --key-file=./auth/service_acct_key.json
and it shows this
Activated service account credentials for: [[email protected]]
but when run the server again
node server.js
I still got the error
what is causing this error and how can authenticate correctly ?
CodePudding user response:
With gcloud CLI, you have 2 level of authentication:
- The CLI level
- The Google Cloud Auth library level (Also named ADC, for Application Default Credential)
When you perform the command gcloud auth ....
you are at the CLI level
When you perform the command gcloud auth application-default ...
you are at the ADC level.
In your case, you only set the authentication at the CLI level, and, of course, that authentication isn't detected in your NODE app, that use Google Cloud libraries and search credential at ADC level.
When you use service account key file (that is a bad practice, but too often prosed and shared in tutorial, even on Google Cloud tutorials (...)), you have to set an environment variable GOOGLE_APPLICATION_CREDENTIALS
with the value equals to the absolute path of your service account key file. Try that
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/auth/service_acct_key.json
node server.js
It should work.