Google Dialogflow:“应用程序默认凭据”不可用

Yur*_*uri 6 java google-cloud-platform gcloud-java dialogflow-es

嗨,我遇到了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
Run Code Online (Sandbox Code Playgroud)

不起作用

推杆

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json
Run Code Online (Sandbox Code Playgroud)

在.bash_profile中

不起作用

在我的Java代码中:

System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/my.json");
GoogleCredential credential = GoogleCredential.getApplicationDefault();
Run Code Online (Sandbox Code Playgroud)

不起作用

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

不起作用

通过“ Maven构建>环境>新变量”将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
Run Code Online (Sandbox Code Playgroud)

真的我无法理解错在哪里。

这是由于上述错误而无法查询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);  
Run Code Online (Sandbox Code Playgroud)

发生例外

SessionsClient sessionsClient = SessionsClient.create();
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Xtr*_*der -2

与问题中提到的不同,以这种方式提供时我已经获得了有效的“凭据”

Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Run Code Online (Sandbox Code Playgroud)

但同样的方法FirestoreOptions失败并出现上述错误“应用程序默认凭据不可用”。

注意:我愿意不通过环境变量配置我的应用程序,而更喜欢在代码中进行配置。还有一个原因 - 我需要来自一个应用程序的多个不同连接。

在调试 Google 的代码后,我发现他们不使用提供的凭据,而是调用getCredentialsProvider()提供的选项。我认为这是他们实现中的错误,但解决方法相当简单:

  1. 创建自己的 CredentialsProvider
  2. 将其设置到 FirestoreOptions 中

虚拟样本:

public static class MyCredentialsProvider implements CredentialsProvider {
    GoogleCredentials credentials;

    public MyCredentialsProvider(GoogleCredentials credentials) {
        this.credentials = credentials;
    }

    @Override
    public Credentials getCredentials() throws IOException {
        return credentials;
    }
}

...

FirestoreOptions.Builder builder = FirestoreOptions.newBuilder()
        .setProjectId(serviceAccount.project_id)
        .setCredentialsProvider(new MyCredentialsProvider(credentials));
Run Code Online (Sandbox Code Playgroud)