You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot对接GCP语音转文本服务时遇身份验证错误

Spring Boot集成GCP语音转文字服务认证失败问题

错误信息

Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.
    at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:152) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:124) ~[google-auth-library-oauth2-http-1.22.0.jar:1.22.0]
    at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:70) ~[gax-2.42.0.jar:2.42.

核心代码

public String recognizeVoice(String encodedfile) throws IOException
{
    System.out.println("encoded file inside recognize Voice  "+ encodedfile);
    SpeechClient speechClient = SpeechClient.create(); // 报错行
    System.out.println("client created succesflly" );
    // The path to the audio file to transcribe
    String gcsUri = "src/main/resources/samplesSounds/punjabiAudioSpeech.wav";

    // Builds the sync recognize request
    RecognitionConfig config =
            RecognitionConfig.newBuilder()
                             .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                             .setSampleRateHertz(16000)
                             .setLanguageCode("pa-Guru-IN")
                             .setModel("command_and_search")
                             .setAudioChannelCount(2)
                             .build();

    RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();

    // Performs speech recognition on the audio file
    RecognizeResponse response = speechClient.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();

    for (SpeechRecognitionResult result : results) {
        // There can be several alternative transcripts for a given chunk of speech. Just use the
        // first (most likely) one here.
        SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
        System.out.printf("Transcription: %s%n", alternative.getTranscript());
    }
    return "FAILED";
}

已尝试的配置

在Spring Boot的application.properties中配置:

spring.cloud.gcp.credentials.location=key.json

其中key.json为GCP服务账号密钥文件,但仍报错,询问其他访问该云服务的方式。


解决方案

1. 显式加载凭据创建SpeechClient

绕过默认凭据查找逻辑,直接在代码中加载服务账号密钥:

public String recognizeVoice(String encodedfile) throws IOException
{
    System.out.println("encoded file inside recognize Voice  "+ encodedfile);
    
    // 加载服务账号密钥(若密钥在resources目录下,使用getResourceAsStream)
    GoogleCredentials credentials = GoogleCredentials.fromStream(getClass().getResourceAsStream("/key.json"));
    SpeechSettings speechSettings = SpeechSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
            .build();
    
    // 使用自定义配置创建SpeechClient
    SpeechClient speechClient = SpeechClient.create(speechSettings);
    System.out.println("client created succesflly" );
    
    // 后续语音识别代码保持不变...
}

2. 设置系统环境变量

通过环境变量指定凭据文件路径,让GCP SDK自动读取:

  • Windows:命令行执行
    set GOOGLE_APPLICATION_CREDENTIALS=C:\绝对路径\key.json
    
  • Linux/macOS:终端执行
    export GOOGLE_APPLICATION_CREDENTIALS=/绝对路径/key.json
    

设置完成后重启应用,SpeechClient.create()即可自动识别凭据。

3. 修正Spring Cloud GCP配置

若密钥文件放在src/main/resources目录下,需在配置中添加classpath:前缀:

spring.cloud.gcp.credentials.location=classpath:key.json

同时确保项目已引入Spring Cloud GCP语音服务依赖:

Maven依赖

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-speech</artifactId>
</dependency>

Gradle依赖

implementation 'com.google.cloud:spring-cloud-gcp-starter-speech'

4. 验证服务账号权限

确认GCP服务账号已拥有Cloud Speech-to-Text Admin或Cloud Speech-to-Text Editor权限,避免因权限不足导致认证失败。


内容的提问来源于stack exchange,提问作者Gurkirat Singh Guliani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 17:01:14