[英]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.