javaauthenticationgoogle-text-to-speech

How to authenticate for Google Text To Speech from a Java app?


I am trying to use Googles Text to Speech from a Java application - no web. I have a hard time to understand Googles pages and in particular the different authentication mechanisms. After reading https://stackoverflow.com/questions/46287267/how-can-i-get-the-file-service-account-json-for-google-translate-api I am even more confused. To my understanding, the below code should work, given that I would have the json file with my credentials. However, I don't know where or how to create this json file - I can't test the application due to the missing json file. In order to make below code work, my questions are:

Here is the code

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.texttospeech.v1.AudioConfig;
import com.google.cloud.texttospeech.v1.AudioEncoding;
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
import com.google.cloud.texttospeech.v1.SynthesisInput;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1.TextToSpeechSettings;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
import com.google.protobuf.ByteString;

public class GoogleTextToSpeech {


    protected TextToSpeechClient textToSpeechClient;
    VoiceSelectionParams voice;
    AudioConfig audioConfig;

    public GoogleTextToSpeech() {

        String jsonPath = "tts.json";

        try {

            CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream(jsonPath)));

            TextToSpeechSettings settings = TextToSpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();

            textToSpeechClient = TextToSpeechClient.create(settings);

            voice = VoiceSelectionParams.newBuilder()
                    .setLanguageCode("en-US")
                    .setSsmlGender(SsmlVoiceGender.FEMALE)
                    .build();
            
            audioConfig = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.LINEAR16).build();        // This relates to a wav file

        } catch (IOException e1) {
            System.err.println(e1.getMessage());
            e1.printStackTrace();
        }

    }


    public void speak(String message) {

        SynthesisInput input = SynthesisInput.newBuilder().setText(message).build();
        SynthesizeSpeechResponse response = textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
        ByteString audioContents = response.getAudioContent();

        // Write the response to the output file.
        try (OutputStream out = new FileOutputStream("output.wav")) {
            out.write(audioContents.toByteArray());
            System.out.println("Audio content written to file \"output.wav\"");
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

}

Thank you in advance.


Solution

  • One need to create a service account in advance. Next go to "IAM and administration" page - since my UI is German, I am guessing the wording - on the left menu select "service account". In the heading menu select "key" and a little below there'll be a button "add key".

    The whole topic is also explained here: https://cloud.google.com/text-to-speech/docs/before-you-begin#creating_a_json_key_for_your_service_account