Google Speech API credentials
Asked Answered
H

4

7

I am trying to do some test using the Google Speech API but from outside Google Cloud. In the older beta version I was able to specify a credentials file but now I am unable to find this option in the SpeechClient class.

How can I go about specifying the authentication keys using the Google Speech API Java library?

Hewitt answered 7/6, 2017 at 10:4 Comment(0)
B
11

Some of the classes from Frank's answer are now deprecated. This is an update to his answer.

CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("path/to/service-account.json")));

SpeechSettings settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
SpeechClient speechClient = SpeechClient.create(settings);
Brownstone answered 14/4, 2018 at 7:54 Comment(1)
If you're using Android Studio don't forget to add the following dependencies: implementation 'io.grpc:grpc-okhttp:1.38.0' implementation 'io.grpc:grpc-stub:1.38.0' compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+Caseate
E
2

Follow-up from issue sent here: https://github.com/GoogleCloudPlatform/java-docs-samples/issues/697#issuecomment-309696984

The way to pass a service account to the SpeechClient is by using the following flow:

CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/service-account.json")));

ChannelProvider channelProvider = SpeechSettings.defaultChannelProviderBuilder().setCredentialsProvider(credentialsProvider).build();

SpeechSettings settings = SpeechSettings.defaultBuilder().setChannelProvider(channelProvider).build();

// Instantiates a client
SpeechClient speech = SpeechClient.create(settings);
Epochmaking answered 20/6, 2017 at 14:23 Comment(0)
R
1

I know this answer is too late. But I also got the issues with versions and deprecated functions. So here is the updated working code.

    InputStream inputStream = Your.class.getResourceAsStream("path/to/the/credentials/file.json");
    GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream);
    CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);

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

    TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(settings);

Hope this helps someone !!!

Remise answered 26/1, 2020 at 7:48 Comment(0)
A
0

I ended up using a post request with base64 of the audio file then added the Google api key in the request.

                var base64 = Convert.ToBase64String(File.ReadAllBytes(file));

                dynamic request = new
                {
                    config = new
                    {
                        encoding = "LINEAR16",
                        sampleRateHertz = 8000,
                        languageCode = "en-US",
                        enableWordTimeOffsets = false
                    },
                    audio = new
                    {
                        content = base64
                    }
                };

    var json = JsonConvert.SerializeObject(request);
    var requestJson = StringContent(json, Encoding.UTF8, "application/json");

                var client = new HttpClient();

                var speechToText = "";
                var response = await client.PostAsync($"https://speech.googleapis.com/v1/speech:recognize?key=GOOGLE-KEY", requestJson);
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();

                    var converted = JsonConvert.DeserializeObject<GcpSpeechApiResponseModel>(content);

                    if (converted != null) {
                        foreach (var result in converted.Results)
                        {
                            foreach (var alternative in result.Alternatives)
                            {
                                speechToText = speechToText + alternative.Transcript;
                            }
                        }
                    }
                }

                return speechToText;
Account answered 19/1, 2018 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.