java.io.IOException:环境变量 GOOGLE_APPLICATION_CREDENTIALS 必须定义为指向定义凭据的文件

java.io.IOException:Environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials

我想在 google 和 android 应用中使用 google 云语音 api 我正在关注由 google [=13] 提供的 link =]

我已经尝试了link https://developers.google.com/accounts/docs/application-default-credentials中提供的解决方案,但仍然无效。

Windows环境变量用户和系统都有GOOGLE_APPLICATION_CREDENTIALS=C:\Users\ANSH\Downloads\credential.json

所有必要的导入都存在。

 private void recognizeSpeech(){
    try{
        SpeechClient speechClient = SpeechClient.create();
        String languageCode="en-US";
        int sampleRateHertz=16000;
        RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.LINEAR16;
        RecognitionConfig config =
                RecognitionConfig.newBuilder()
                        .setLanguageCode(languageCode)
                        .setSampleRateHertz(sampleRateHertz)
                        .setEncoding(encoding)
                        .build();
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path);
        ByteString content = ByteString.copyFrom(data);
        RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(content).build();
        RecognizeRequest request =
                RecognizeRequest.newBuilder().setConfig(config).setAudio(audio).build();
        RecognizeResponse response = speechClient.recognize(request);
        for (SpeechRecognitionResult result : response.getResultsList()) {
            // First alternative is the most probable result
            SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
            outputText.setText(alternative.getTranscript());
        }
    }
    catch (Exception e){
        Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
    }
}

credential.json 包含使用服务帐户下载的密钥

ENVIRONMENT VARIABLE IMAGE

https://cloud.google.com/speech-to-text/docs/libraries 提到一个注释。

注意:云 Java 客户端库当前不支持 Android。

是说不能在android studio中使用吗?如果有怎么用?

您可能想查看此 github link 以查看异常是如何解决的,但请考虑 Speech To Text 客户端库指定不支持 Android,因为它已发布在评论中。

此外,由于您的问题是关于在 Android 应用程序中设置身份验证时出现的问题,我建议您使用 OAuth 2.0 客户端 ID。您将需要生成一个 OAuth 2.0 client ID from the Google Console and then follow Android Instructions for passing requestIdToken or requestServerAuthCode in the GoogleSignInOptions。另外,请记住,这不能保证 Speech API 和 Android 之间的正确集成,因此如果您得到这项工作,请与我们分享。