0
votes

I use the GCP Text-to-Speech API to create greetings and interactive menus that are played to phone customers who call into businesses. I have successfully used the Text-to-Speech API in the past using this Google quick-start: Google Cloud Text-to-Speech I used the same quick-start guide again for a new organization and project but I keep getting an invalid key error which I have been unable to debug. Here are the commands I have used to invoke the service and the results:

# curl -X POST  \ 
-H "Authorization: Bearer $(gcloud auth application-default print-access-token)”\ 
-H "Content-Type: application/json; charset=utf-8"  \  
-d @request.json  \
https://texttospeech.googleapis.com/v1/text:synthesize > synthesize-output-base64.txt

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

I tried again with a different authentication header:

# curl -X POST   \ 
-H "X-Goog-Api-Key:***my_API_key***.json"   \
-H "Content-Type: application/json; charset=utf-8"  \ 
-d @request.json  \ 
https://texttospeech.googleapis.com/v1/text:synthesize > synthesize-output-base64.txt

{
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "texttospeech.googleapis.com"
        }
      }
    ]
  }
}

I have re-verified the following configurations:

The Text-to-Speech API is enabled for my project.

The credential (service account) I am attempting to use is listed with the Text-to-Speech API as compatible. I verified that that service account key is the key that was used in the attempted authentications.

I compared my configuration on GCP with another working configuration I manage for a different company. There are no differences I can see.

I compared my key and the other company's working key and they are identical except for the specific user data.

The billing account for the project is active with valid payment card in place.

These two links were helpful but did not clear up the problem: Google Cloud Text-to-Speech API - permission error Google Cloud Text to Speech INVALID API KEY

Any help on this issue would be very appreciated!

2
Verify that gcloud auth application-default print-access-token is actually returning a valid OAuth Access Token. For your second example, this is not correct -H "X-Goog-Api-Key:***my_API_key***.json" Is the .json ending a typo? If yes, correct your details. - John Hanley
If the CLI does not print an error and prints a long string it is most likely a valid access token. There are several variations but it should start with something similar to ya29.. (four characters and a period). For your first example, I recommend assigning the token creation output to a shell variable and then using that as your Bearer token. Helps with debugging. - John Hanley
To validate a Google OAuth Access Token: curl -H "Authorization: Bearer TOKEN" https://www.googleapis.com/oauth2/v3/tokeninfo - John Hanley
***my_API_key*** is where I put my actual key. I probably could have made a better choice of how to hide my key...The error code I am getting says I have an invalid key. Is there some configuration error I probably have made in setting this up? - bruce57
Did you setup the service account with the CLI? Look at Step 6 in my article: jhanley.com/… - John Hanley

2 Answers

0
votes
  1. I enabled the Cloud Text-to-Speech API

  2. I created a service account with Editor role.

  3. Note: Ensure you have set the GOOGLE_APPLICATION_CREDENTIALS environment variable to your service account private key file path.

  4. Save the request body in a file called request.json,

  5. Execute the following command:

curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://texttospeech.googleapis.com/v1/text:synthesize
  1. Response body
{
  "audioContent": "//NExAARcrn0ABhEudAgAjuem7vohckY3IAFgWvxrBkAD//ARvqc/QhCZ/6EJO6E9Dvo2cjHPQhLfWQ/rTU7yEznsoGBn8hJz5xbQiOgEfwDzD1DVARLPlzGNXylbM8s//NExA0SCnoAAUIQAd8vlrM9DPR//6t5fpUpS
sk3MbzGUvMhqOpdSsjmcpeY2Zz ...................
}

Everything worked as expected

0
votes

The solution to this problem is to install the Google Cloud SDK.

The CLI was missing so the returned output from gcloud auth application-default print-access-token was an error.