java - 谷歌对话流 : The Application Default Credentials are not available

标签 java google-cloud-platform dialogflow-es gcloud-java

您好,我有一个关于 Google Cloud 的 Java SDK 库的问题。

我需要查询 Dialogflow V2 API,我正在使用这个 SDK ( https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master )

我已按照说明将 GOOGLE_APPLICATION_CREDENTIALS 设置为环境变量。

我做了几次尝试(我使用的是 Mac):

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

不起作用

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

在 .bash_profile 中

不起作用

在我的 Java 代码中:

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

不起作用

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

不起作用

通过“Maven build > Environment > New variable”将 GOOGLE_APPLICATION_CREDENTIALS 作为环境变量放入 Eclipse 并重启 IDE

不起作用

总是出现同样的错误:

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

真的我不明白哪里出了问题。

这是用于查询由于上述错误而从未到达的 Dialogflow 的代码片段:

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);  

异常发生在

SessionsClient sessionsClient = SessionsClient.create();

提前致谢。

最佳答案

解决方案是将 GOOGLE_APPLICATION_CREDENTIALS 设置为应用服务器的环境变量。

关于java - 谷歌对话流 : The Application Default Credentials are not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50294866/

相关文章:

java - 如何将文件内容打印到 GUI

dialogflow-es - actions-on-google-dialogflow-session-entities-plugin 在dialogflow的内联编辑器中不起作用

java - 在适用于Windows 10 Home 64bit的Android Studio 3.1.3中运行构建失败

java - Spring:在语言环境 'en_US' 的代码下找不到消息

kubernetes - dnsConfig 在 GKE 中被跳过

docker - 如何正确配置 Dockerfile 以在 Google Cloud Run 上运行?

flutter - 如何在 flutter 中创建带有响应选项的聊天机器人?

python - 如何使用 python 在 Messenger-Dialogflow 聊天机器人中将 Mysql 查询结果作为 Excel 报告发送

java - 如何在java中实现后端计数计时器的前端 View ?

python - Google Cloud Firestore 触发器未检测到对数据库的写入