Jonah
Jonah

Reputation: 16212

Using Google's TextToSpeech API on Heroku

I am trying to use Google's TextToSpeech REST API on heroku.

I have a service account keyfile, which works fine locally, but I cannot figure out how I could make it work on Heroku, where the Google Cloud SDK is not installed. That is, consider this sample curl call from the quickstart:

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -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-output.txt

which depends on the bash command $(gcloud auth application-default print-access-token), which in turn depends on the google cloud SDK being installed (I have the mac version installed locally).

I looked into using the ruby client library, which I believe handles the authentication under the hood if it has the keyfile via the GOOGLE_CLOUD_KEYFILE_JSON env variable, but it does not appear to support TextToSpeech.

How can I make TextToSpeech API calls on Heroku?

Upvotes: 2

Views: 1053

Answers (1)

Grokify
Grokify

Reputation: 16334

You really just need to use the service account credentials to get an access token via OAuth 2.0. You can do this manually or use an SDK. Google as a set of Speech-to-Text Client Libraries at the link here. There are a few languages supported including Ruby, as well as C#, Go, Java, Node.js, PHP and Python:

You can install the Ruby SDK with the following:

$ gem install google-cloud-speech

Then add the filepath to your credentials files as an environment variable:

$ export GOOGLE_APPLICATION_CREDENTIALS="/path/to[FILE_NAME].json"

I use Go and have a Google Text-to-Speech example here calling both text.synthesize (like your example) and voices.list. This example stores the Google key file contents in an environment variable which makes it easy to read from a Heroku instance. While this uses the google/google-api-go-client SDK, I just noticed and will consider upgrading to GoogleCloudPlatform/google-cloud-go now.

This retrieves an access token and then instantiates the Google Go Client SDK:

The Google Go SDK supports Text-to-Speech here:

Go is easy to get on to Heroku as well using godep as documented here:

I also have a scaffolding app that will create the files you need to enable the "Deploy to Heroku" button for a Go app here:

If you use Go and have any questions let me know.

Upvotes: 2

Related Questions