6
votes

I am attempting to set up Google Cloud Text-to-Speech API following these instructions - https://cloud.google.com/text-to-speech/docs/quickstart I have successfully followed steps 1-6 for setting up Google SDK and authenticating with service account credentials. However, when I attempt to run the sample HTTP request for synthesising speech I receive the following error:

Cloud Text-to-Speech API has not been used in project usable-auth-library before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/texttospeech.googleapis.com/overview?project=usable-auth-library then retry.

When following the link in the error message it leads to the following page:

The API "texttospeech.googleapis.com" doesn't exist or you don't have permission to access it.

I am grateful for help.

3
It looks like your code is trying to authenticate using the usable-auth-library project instead of your actual project. My guess is that's due to how the credentials are being used. Have you tried setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the location of your service account JSON file instead?Jon Skeet
Thanks for the reply. I have set GOOGLE_APPLICATIONS_CREDENTIALS as you suggest but still get the same error. Does the gcloud command in the curl request (curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token)) need to be changed perhaps?MKB
Possibly - if you take the application-default out of there, does that help? If you were planning on using a client library, it might be worth skipping straight to that, as that may make it easier to make sure you're really using the service account credentials.Jon Skeet
Thank you for the reply. This has been resolved now, by rerunning the gcloud auth activate-service-account step after setting the GOOGLE_APPLICATION_CREDENTIALS environment variable.MKB
to confirm I did also take out 'application-default' as suggestedMKB

3 Answers

2
votes

I easier to integrate in most plarforms using the API key instead of the service account key that Google recomends on their docs.

These are all the steps you need to get to the the API key

  1. Create a project (or use an existing one) in the Cloud Console.
  2. Make sure that billing is enabled for your project.
  3. Enable the Text-to-Speech API.
  4. Create an API key.

You'll probably only need the last step (if you followed all the steps correctly like you said).

And then you can use the curl command like so

Curl -H "X-Goog-Api-Key: PUT_YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json; charset=utf-8" \
  --data "{
    'input':{
      'text':'Android is a mobile operating system developed by Google,
         based on the Linux kernel and designed primarily for
         touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
      'languageCode':'en-gb',
      'name':'en-GB-Standard-A',
      'ssmlGender':'FEMALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt
1
votes

In case this helps anyone, I ran into this error after adding the API through the Google console to an existing Google Service Account with JWT credentials.

I followed the link to the Quickstart Protocol and was able to get it working On the page below, I clicked on Enable the API.

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

Text-to-Speech Quickstart

On the next page, I clicked the Create a project drop down and selected an existing project. There was no need to get new credentials.

enter image description here

I'm using Go and was then able to run my code using golang.org/x/oauth2/google.

0
votes

Once you created an API key, here is your curl one-liner that fetches the result, parses it, formats it and decodes it.

  1. Generate API key here https://console.cloud.google.com/apis/credentials?showWizardSurvey=true
  2. Install jq JSON parser
  3. Run:
curl -H "X-Goog-Api-Key: APIKEYHERE" -H "Content-Type: application/json; charset=utf-8" --data "{ 'input':{ 'text':'I\'ve added the event to your calendar.' }, 'voice':{ 'languageCode':'en-gb', 'name':'en-GB-Standard-A', 'ssmlGender':'FEMALE' }, 'audioConfig':{ 'audioEncoding':'MP3' } }" "https://texttospeech.googleapis.com/v1/text:synthesize" | jq '.audioContent' | cut -d "\"" -f 2 > encodedOutput && base64 --decode encodedOutput > texttospeech.mp3