Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Dialogflow: The Application Default Credentials are not available

Hi I've an issue with Java SDK library for Google Cloud.

I need to query Dialogflow V2 API and I'm using this SDK (https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master)

I've followed the instructions in order to set GOOGLE_APPLICATION_CREDENTIALS as environment variable.

I did several attempts (I'm using a Mac):

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

doesn't work

putting

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

in .bash_profile

doesn't work

In my Java code:

System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/my.json");
GoogleCredential credential = GoogleCredential.getApplicationDefault();

doesn't work

String jsonPath = "/path/to/my.json";
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

doesn't work

putting GOOGLE_APPLICATION_CREDENTIALS as environment variable in Eclipse by "Maven build > Environment > New variable" and restarted IDE

doesn't work

The same error always occurs:

An error occurred during Dialogflow http request: 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.
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

Really I can't undestand what is wrong.

This is the code snippet to query Dialogflow never reached due to the above error:

SessionsClient sessionsClient = SessionsClient.create();
SessionName session = SessionName.of("my-project-id", sessionId);
InputStream stream = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
QueryInput queryInput = QueryInput.newBuilder().build().parseFrom(stream);
DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);  

Exception is raised on

SessionsClient sessionsClient = SessionsClient.create();

Thanks in advance.

like image 666
HK15 Avatar asked May 11 '18 14:05

HK15


Video Answer


1 Answers

The solution is to set GOOGLE_APPLICATION_CREDENTIALS as environment variable for the Application Server.

like image 98
Jinjun Avatar answered Nov 03 '22 09:11

Jinjun