0

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 :

  • Created a Google Cloud Service Account and enabled Vision API.
  • Placed vision-service-account.json inside src/main/resources.
  • Tried different ways to set credentials in application.yml:

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
  • Created a configuration class to load credentials:
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)
5
  • I'm not a Java (Spring Boot) dev but am familiar with Google Cloud. Generally (!) with Application Default Credentials (ADCs) you don't need to configure your code to use credentials because ADC attempts to automate credentials discovery and authentication. If your code is running locally, you can often use your user credentials. Commented Mar 6 at 16:48
  • If your code is running on a Google compute service (e.g. Cloud Run, Compute Engine), your code will authenticate using the Metadata service. Commented Mar 6 at 16:48
  • 1
    If you're using any (!) of the ADC approaches, you should be able to remove the explicit references to the Service Account that you're attempting to provide and preferably use one of the above solutions or for testing purposes only export GOOGLE_APPLICATION_CREDENTIALS=/path/to/vision-service-account.json Commented Mar 6 at 16:50
  • 1
    you comment helped a lot thanks, i just had to add GOOGLE_APPLICATION_CREDENTIALS to environment: on the docker compose file. Commented Mar 7 at 1:15
  • 1
    @DazWilkin i will reply in case other people face the same issue Commented Mar 7 at 1:21

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

4 Comments

You should not store the Service Account key in the container. Anyone with the container has access to the key. You should mount the key from the environment.
@DazWilkin does that mean i have to remove the volumes line ?
My apologies, no you're doing it correctly. I read "stored inside the container" and didn't read your Docker Compose.
It's okay. I changed it so it doesn't confuse readers. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.