google-cloud-platformgoogle-text-to-speech

Can't access Google Cloud Text To Speech with API Key


I am trying to use cloud text to speech to add accessibility to a web-based game. It is being built in Unity for WebGL.

Eventually I am firing a web request towards the following URL: string url = "https://texttospeech.googleapis.com/v1/text:synthesize?key=" + apiKey;, including the proper API key.

However I always receive 403 Forbidden errors when trying to synthesize text:

webgl.framework.js.gz:3 [Google TTS] Received response FORBIDDEN from Google. Please check whether your API key restrictions might be blocking this call. You might have set this to only be allowed from your website. If so, you might want to create an unrestricted Editor-only API key which you're using for development.

webgl.framework.js.gz:3 [Google TTS] Error Code: 403 - HTTP/1.1 403 Forbidden

POST https://texttospeech.googleapis.com/v1/text:synthesize?key=<key redacted> 403 (Forbidden)

The key seems to be configured correctly, as seen below:

API Key configuration; restricted to certain websites and TTS API:

API Key configuration; restricted to certain websites and TTS API

It is set to restrict to the website (I triple checked the URLs, they are matching and it doesn't work without http referrer restrictions either) and restricted to Cloud Text To Speech API.

The API is enabled as well and I can see the requests come through but they always throw an error:

Enabled API, 100% error rate:

Enabled API, 100% error rate

There also is a valid and configured billing account assigned to the project.

I saw various tutorials doing the same but I can not get it to work... Do API keys no longer function for cloud TTS?


Solution

  • Google Cloud to Speech still supports API key (OAuth is another option). Your issue is likely related to Google API key application restriction configuration, when the request is sent from WebGL in Unity. You can validate your API key by running a direct curl command:

    curl -X POST -H "Content-Type: application/json" -d @request.json https://texttospeech.googleapis.com/v1/text:synthesize?key=[your-api-key]
    

    request.json can be a simple request body object:

    {
        "input": {
            "text": "Hello Google Speech to Text."
        },
        "voice": {
            "languageCode": "en-US",
            "name": "en-US-Standard-A",
            "ssmlGender": "FEMALE"
        },
        "audioConfig": {
            "audioEncoding": "MP3"
        }
    }
    

    You may need to temporarily change your API key application restriction to none or use the IP address of your test machine. This will tell whether your API key works or not.

    If that works, you might want to dig a bit more into how WebGL sends REST request. For example, does it alter the website domain name or sub-domain names. If so, it will cause the Google API throwing 403 Forbidden error. (Disclaimer: I am no WebGL SME)