Home > OS >  GCP text-to-Speech API auth issue
GCP text-to-Speech API auth issue

Time:06-13

I was trying the above api in postman. Here is the request json:

{
  "input":{
    "text":"Flutter is awesome!"
  },
  "voice":{
    "languageCode":"en-gb",
    "name":"en-GB-Standard-A",
    "ssmlGender":"FEMALE"
  },
  "audioConfig":{
    "audioEncoding":"MP3"
  }
}

for auth, i chose Bearer in postman auth and first executed the following command in my terminal to get the token:

gcloud auth application-default print-access-token

i pasted this token in auth header, and i received the following response :

{
    "error": {
        "code": 403,
        "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.",
        "status": "PERMISSION_DENIED",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.ErrorInfo",
                "reason": "SERVICE_DISABLED",
                "domain": "googleapis.com",
                "metadata": {
                    "consumer": "projects/12345678910",
                    "service": "texttospeech.googleapis.com"
                }
            }
        ]
    }
}

I am very new to GCP in general and don't know how to navigate this issue. For additional context, i am trying to make a REST API call where i send the text and get a base64encoded string containig audio back. Any help is appreciated.

CodePudding user response:

This is confusing/complex but the error is helpful:

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.

NOTE You can try this method using Google's APIs Explorer at this link text.synthesize.

The issue is that gcloud is an OAuth2 application and tokens issued by gcloud either using gcloud auth print-[access|identity]-token and gcloud auth application-default print-access-token are issued against a Google-managed project (that Google provides for gcloud) and -- importantly -- not one of your own projects.

Google wants to provide gcloud for its users but does not want to provide arbitrary API access (for free) to its users. Hence the "not supported" part of the error.

The solution (as described) is that you should:

  1. Use (or create) your own Google Project
  2. Enable the Text-to-Speech service (API) in this project
  3. Create a Service Account and key
  4. gcloud auth activate-service-account providing the Service Account key
  5. gcloud auth print-access-token to get an access token to invoke the API

See the following link for the steps:

https://cloud.google.com/text-to-speech/docs/libraries

  • Related