Google语音API凭据

时间:2017-06-07 10:05:00

标签: java google-speech-api

我正在尝试使用Google Speech API进行一些测试,但是来自Google Cloud外部。在较旧的测试版中,我能够指定凭据文件,但现在我无法在TypeArg类中找到此选项。

如何使用Google Speech API Java库指定身份验证密钥?

4 个答案:

答案 0 :(得分:2)

Frank的答案中的一些课程现已弃用。这是对他答案的更新。

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);

答案 1 :(得分:1)

此处发送的问题的后续跟踪:https://github.com/GoogleCloudPlatform/java-docs-samples/issues/697#issuecomment-309696984

将服务帐户传递给SpeechClient的方法是使用以下流程:

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);

答案 2 :(得分:1)

我知道这个答案为时已晚。但是我也遇到了版本和不推荐使用的功能的问题。这是更新的工作代码。

    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);

希望这对某人有帮助!

答案 3 :(得分:0)

我最终使用了带有音频文件base64的发布请求,然后在请求中添加了Google api密钥。

                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;