Vaishnavi Killekar
Vaishnavi Killekar

Reputation: 503

GCP Dialogflow Java-SpringBoot authentication - Application Default Credentials are not available

I need to connect to GCP Dialogflow for detecting intents. I'm using SpringBoot with Java to connect to Dialogflow. This application runs in Pivotal Cloud Foundry (PCF) and I don't know how GOOGLE_APPLICATION_CREDENTIALS environment variable can be set there.

All suggested solutions here use either gcloud, Google SDK or the app runs in Google Compute Engine. These solutions don't work for my architecture.

GoogleCredentials is always null even if it is set as suggested below -

GoogleCredentials.fromStream(new FileStream("creds.json"));

OR

GoogleCredentials creds = null;
try {
    creds = GoogleCredentials.fromStream(new FileInputStream("creds.json"));
} catch (Exception e) {
    e.printStackTrace();
}

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...

Upvotes: 0

Views: 301

Answers (1)

Vaishnavi Killekar
Vaishnavi Killekar

Reputation: 503

I found the solution to the problem after more research. The whole time I was trying to do this in my SpringBoot app. It was right there, but I didn't think much of it when I first came across it earlier.

The environment variable in PCF can be set via cf CLI. All I had to do was add an additional command to the pipeline and that worked perfectly -

cf set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE

Sources - https://cli.cloudfoundry.org/en-US/v6/set-env.html https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html

P.S. If a new app is being pushed to PCF, please verify before running above command since APP_NAME won't be found in that case.

Upvotes: 1

Related Questions