I'm integrating Google Cloud Vision API into my Spring Boot app, but I keep getting the error:
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.
What I Have Done :
spring:
cloud:
gcp:
project-id: keraa-notification # My Google Cloud project ID
credentials:
location: classpath:vision-service-account.json
or
google:
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)
I had to set the GOOGLE_APPLICATION_CREDENTIALS
in mydocker-compose.yml
file by adding it as an environment variable. This allows your application to authenticate with Google services using a service account key.
version: "3"
services:
myproject:
image: myproject
environment:
- _JAVA_OPTIONS=-Xmx512m -Xms256m
- SPRING_PROFILES_ACTIVE=prod,api-docs
- GOOGLE_APPLICATION_CREDENTIALS=/app/resources/vision-service-account.json
volumes:
- ./firebase-service-account.json:/app/resources/vision-service-account.json:ro