首页 > 解决方案 > Google Dialogflow:应用程序默认凭据不可用

问题描述

嗨,我对 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();

提前致谢。

标签: javagoogle-cloud-platformdialogflow-esgcloud-java

解决方案


解决方案是将 GOOGLE_APPLICATION_CREDENTIALS 设置为 Application Server 的环境变量。


推荐阅读