Spring Boot集成Google Cloud Vision API时默认凭据不可用问题求助
问题:Spring Boot集成Google Cloud Vision API时凭据不可用
报错信息
java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
已执行操作
- 创建了Google Cloud服务账号并启用了Vision API;
- 将
vision-service-account.json放置在src/main/resources目录下; - 尝试两种
application.yml配置方式:
方式一:
方式二:spring: cloud: gcp: project-id: keraa-notification # My Google Cloud project ID credentials: location: classpath:vision-service-account.jsongoogle: cloud: credentials: path: vision-service-account.json - 创建了配置类以加载凭据,但仍出现异常:
2025-03-06 14:06:20 java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. 2025-03-06 14:06:20 at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127) 2025-03-06 14:06:20 at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:129) 2025-03-06 14:06:20 at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:101) 2025-03-06 14:06:20 at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:70) 2025-03-06 14:06:20 at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:168) 2025-03-06 14:06:20 at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:115) 2025-03-06 14:06:20 at com.google.cloud.vision.v1.stub.ImageAnnotatorStubSettings.createStub(ImageAnnotatorStubSettings.java:163) 2025-03-06 14:06:20 at com.google.cloud.vision.v1.ImageAnnotatorClient.<init>(ImageAnnotatorClient.java:160) 2025-03-06 14:06:20 at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:142) 2025-03-06 14:06:20 at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:133) 2025-03-06 14:06:20 at com.keraa.api.service.ImageModerationService.isImageSafe(ImageModerationService.java:21) 2025-03-06 14:06:20 at com.keraa.api.web.rest.ProductResource.createProduct(ProductResource.java:116)
解决方案
1. 确认依赖是否正确引入
如果使用Spring Cloud GCP Starter,确保pom.xml中引入正确依赖:
<dependency> <groupId>com.google.cloud</groupId> <artifactId>spring-cloud-gcp-starter-vision</artifactId> </dependency>
若直接使用Google Cloud Vision原生客户端,需确保依赖正确,此时Spring Cloud GCP配置不会自动生效,需手动加载凭据。
2. 确保Spring Cloud GCP配置生效
使用Spring Cloud GCP Starter时,application.yml正确配置如下:
spring: cloud: gcp: project-id: keraa-notification credentials: location: classpath:vision-service-account.json
同时检查打包后的target/classes目录下是否存在vision-service-account.json,确保文件被正确加载。
3. 手动加载凭据(原生客户端方式)
直接使用ImageAnnotatorClient时,绕过默认凭据提供者,手动加载指定文件:
import com.google.cloud.vision.v1.ImageAnnotatorClient; import com.google.cloud.vision.v1.ImageAnnotatorSettings; import com.google.auth.oauth2.GoogleCredentials; import org.springframework.core.io.ClassPathResource; // 在服务类中初始化客户端 public ImageAnnotatorClient createImageAnnotatorClient() throws IOException { GoogleCredentials credentials = GoogleCredentials.fromStream( new ClassPathResource("vision-service-account.json").getInputStream()); ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder() .setCredentialsProvider(() -> credentials) .build(); return ImageAnnotatorClient.create(settings); }
4. 检查环境变量冲突
若系统中存在GOOGLE_APPLICATION_CREDENTIALS环境变量,会覆盖配置文件设置,可暂时移除该变量,或确保其指向正确的文件路径。
5. 正确编写自定义配置类
若使用自定义配置类,确保能正确加载凭据并注入客户端:
import com.google.cloud.vision.v1.ImageAnnotatorClient; import com.google.cloud.vision.v1.ImageAnnotatorSettings; import com.google.auth.oauth2.GoogleCredentials; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import java.io.IOException; @Configuration public class GoogleVisionConfig { @Bean public ImageAnnotatorClient imageAnnotatorClient() throws IOException { GoogleCredentials credentials = GoogleCredentials.fromStream( new ClassPathResource("vision-service-account.json").getInputStream()); ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder() .setCredentialsProvider(() -> credentials) .build(); return ImageAnnotatorClient.create(settings); } }
之后在服务类中注入ImageAnnotatorClient即可使用。
内容的提问来源于stack exchange,提问作者Charfeddine Mohamed Ali
相关产品推荐
相关产品推荐

