简体   繁体   English

GCP DialogFlow API 错误:“应用程序默认凭据不可用”

[英]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:尝试在 Spring 应用程序中使用 Google DialogFlow API 时出现以下错误:

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.我成功执行了https://cloud.google.com/dialogflow/es/docs/quick/setup#windows中的所有步骤,并且当我运行命令gcloud auth application-default print-access-token来测试身份验证,它工作正常。 However from my spring app, I keep getting an error when attempting to use the API.但是,在我的 spring 应用程序中,我在尝试使用 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作为参考,我使用以下内容将我的 Spring 应用程序连接到 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:编辑:我手动将 GOOGLE_APPLICATION_CREDENTIALS 添加为环境变量,但现在出现以下错误:

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.添加此代码块作为DetectIntentTexts类中的主要方法。

    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.将此代码放入您的detectIntentTexts方法中。

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)) try (SessionsClient sessionsClient = SessionsClient.create())与此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 .最后,将这些依赖项添加到您的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:这是输出:

代码输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Dialogflow:应用程序默认凭据不可用 - Google Dialogflow: The Application Default Credentials are not available 应用程序默认凭据不可用 - Application Default Credentials not available 无法验证 google recaptcha 企业。 收到错误:java.io.IOException:应用程序默认凭据不可用 - Unable to validate google recaptcha enterprise. getting error: java.io.IOException: The Application Default Credentials are not available Google Cloud Storage API无法在App Engine上获取应用程序默认凭据,以查找Compute Engine错误 - Google Cloud Storage API cannot get Application Default Credentials on App Engine looking for Compute Engine error 应用程序默认凭据不可用。 如果在 Google Compute Engine 中运行,它们就可用。 否则 - java android - The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise - java android 如何使用 Google Cloud API - 应用程序默认凭据进行文本检测 - how to use Google Cloud API - Application Default Credentials for text detection 忽略应用程序默认凭证{0}:使用显式设置API密钥 - Ignoring Application Default Credentials {0}: using explicit setting for API key instead 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置 - The Application Default Credentials are not available with environment variable setup in mac Google Cloud Storage 可以使用 gcp 凭据登录 spring 启动应用程序 - Can use gcp credentials for logging in spring boot application Java应用程序找不到访问GCP发布/订阅的凭据 - Java application cannot find credentials to access GCP Pub/Sub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM