简体   繁体   中英

GCP DialogFlow API error: "The Application Default Credentials are not available"

I am getting the following error when trying to use the Google DialogFlow API in my Spring app:

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.

I followed all the steps fromhttps://cloud.google.com/dialogflow/es/docs/quick/setup#windows successfully, and when I run the command gcloud auth application-default print-access-token to test out the authentication, it works fine. However from my spring app, I keep getting an error when attempting to use the API. For reference, I am using the following in order to connect my Spring app to the DialogFlow API: https://cloud.google.com/dialogflow/es/docs/quick/api#detect-intent-text-java

Any advice?

EDIT: I manually added the GOOGLE_APPLICATION_CREDENTIALS as an envionment variable, but am now getting the following error:

java.lang.NoSuchMethodError: 'com.google.auth.oauth2.ServiceAccountCredentials com.google.auth.oauth2.ServiceAccountCredentials.createWithUseJwtAccessWithScope(boolean)'

Add this block of code to serve as a your main method inside DetectIntentTexts class.

    public static void main(String[] args) {
        
        List <String> myArr = new ArrayList<String>(); 
        myArr.add("Tell me your name");
        try{
        detectIntentTexts("project-id",myArr,"123","en-us");
        }
        catch(Exception e){}
  }

Put this code inside your detectIntentTexts method.

String jsonPath = "<your_json_file_path>";
   GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
       .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
       
   SessionsSettings sessionsSettings =
   SessionsSettings.newBuilder()
           .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
           .build();
   SessionsClient sessionsClient = SessionsClient.create(sessionsSettings);

Also, replace this code:
try (SessionsClient sessionsClient = SessionsClient.create()) with this try (SessionsClient client = SessionsClient.create(sessionsSettings))

Import these packages to your class.

import java.util.ArrayList;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
import com.google.common.collect.Lists;
import com.google.cloud.dialogflow.v2.SessionsSettings;
import com.google.api.gax.core.FixedCredentialsProvider;

Finally, add these dependencies to your pom.xml .

<dependency>
      <groupId>com.google.auth</groupId>
      <artifactId>google-auth-library-oauth2-http</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
        <groupId>com.google.api</groupId>
        <artifactId>gax</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.api</groupId>
        <artifactId>gax-grpc</artifactId>
        <version>2.8.1</version>
    </dependency>

This is the output:

代码输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM