0
votes

I'd like to leverage the new Google TTS using a simple rest request. To that end, I've created a Service Account which downloaded a JSON file containing a private key id, a private key, client email, client id, client_x509_cert_url, etc.

I've also set the environment variable for the system:

 Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", jsonFile);

I then found this sample CURL request to use the WaveNet TTS engine provided by Google:

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" > writtenfile.txt

I want to create a simple C# webrequest from the above CURL request, but the line "gcloud auth application-default print-access-token" is a problem for me. I cannot install a CLI gcloud instance on this machine. Isn't there some way to setup the webrequest authorization access token from the JSON service account file? Surely I don't HAVE to have gcloud installed.

Looking for code sample demonstrating how to convert CURL code to a C# rest request using the service account json file without gcloud.

1
Not sure about your specific case, but I have used something like GoogleCredential googleCredential = GoogleCredential.FromJson(Constants.CRED_JSON); Channel channel = new Channel(SpeechClient.DefaultEndpoint.Host, googleCredential.ToChannelCredentials()); to connect to google speech servicesmahlatse

1 Answers

1
votes

In case you don't want to install the gcloud packages, you can execute REST calls by using API Keys. These keys can be created directly in the GCP console and they can be passed as a parameter through the request header; in this way, you can authenticate to the service without the need of gcloud. Additionally, I suggest you to take a look on this guide that contains the steps required to make REST requests with C#.

You can use this Restlet Client tool to test the following example:

REST content Example

  • Header

https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=<YOUR_API_KEY>

  • Body

{ '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' } }

Finally, in case you want to consume a Service Account JSON file, you can use the Client libraries to create and send your TTS requests to then authenticate them directly from your code; however, this method require to install the TextToSpeech packages.