简体   繁体   English

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

[英]Google Dialogflow: The Application Default Credentials are not available

Hi I've an issue with Java SDK library for Google Cloud.嗨,我对 Google Cloud 的 Java SDK 库有疑问。

I need to query Dialogflow V2 API and I'm using this SDK ( https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master )我需要查询 Dialogflow V2 API,我正在使用这个 SDK ( https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master )

I've followed the instructions in order to set GOOGLE_APPLICATION_CREDENTIALS as environment variable.我已按照说明将 GOOGLE_APPLICATION_CREDENTIALS 设置为环境变量。

I did several attempts (I'm using a Mac):我做了几次尝试(我使用的是 Mac):

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

doesn't work不工作

putting推杆

export GOOGLE_APPLICATION_CREDENTIALS=path/to/my.json

in .bash_profile在 .bash_profile

doesn't work不工作

In my Java code:在我的 Java 代码中:

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

doesn't work不工作

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

doesn't work不工作

putting GOOGLE_APPLICATION_CREDENTIALS as environment variable in Eclipse by "Maven build > Environment > New variable" and restarted IDE通过“Maven build > Environment > New variable”将 GOOGLE_APPLICATION_CREDENTIALS 作为 Eclipse 中的环境变量并重新启动 IDE

doesn't work不工作

The same error always occurs:总是出现同样的错误:

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

Really I can't undestand what is wrong.真的我无法理解出了什么问题。

This is the code snippet to query Dialogflow never reached due to the above error:由于上述错误,这是查询 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);  

Exception is raised on引发异常

SessionsClient sessionsClient = SessionsClient.create();

Thanks in advance.提前致谢。

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

Unlike mentioned in question, I've got 'credentials' working when provided that way与问题中提到的不同,以这种方式提供时,我已经获得了“凭证”

Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

But same approach with FirestoreOptions failed with mentioned error " The Application Default Credentials are not available ".但是与FirestoreOptions相同的方法失败了,并提到了错误“应用程序默认凭据不可用”。

Note: I'm willing to not configure my app via environment variable and prefer doing it in code.注意:我愿意不通过环境变量配置我的应用程序,而是更喜欢在代码中进行配置。 One more reason for that - I need multiple different connections from one app.另一个原因 - 我需要来自一个应用程序的多个不同连接。

After debugging into Google's code I've found that they do not use supplied credentials, they rather call getCredentialsProvider() on provided options.在调试 Google 的代码后,我发现他们不使用提供的凭据,而是在提供的选项上调用getCredentialsProvider() I think this is bug in their implementation, but workaround is rather simple:我认为这是他们实现中的错误,但解决方法相当简单:

  1. Create own CredentialsProvider创建自己的 CredentialsProvider
  2. Set it into FirestoreOptions将其设置为 FirestoreOptions

Dummy sample:虚拟样本:

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

暂无
暂无

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

相关问题 GCP DialogFlow API 错误:“应用程序默认凭据不可用” - GCP DialogFlow API error: "The Application Default Credentials are not available" 应用程序默认凭据不可用 - Application Default Credentials not available 应用程序默认凭据不可用。 如果在 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 recaptcha 企业。 收到错误:java.io.IOException:应用程序默认凭据不可用 - Unable to validate google recaptcha enterprise. getting error: java.io.IOException: The Application Default Credentials are not available 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置 - The Application Default Credentials are not available with environment variable setup in mac Google Cloud Storage 如何使用 Google Cloud API - 应用程序默认凭据进行文本检测 - how to use Google Cloud API - Application Default Credentials for text detection 应用程序默认凭据无法创建Google App Engine服务帐户凭据 - Application Default Credentials fails to create the Google App Engine service account credentials Google Cloud Platform pub / sub Publisher,如何提供默认应用程序凭据以外的凭据 - Google Cloud Platform pub/sub Publisher, how to supply credentials other than default application credentials 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 在Android应用程序中注册Google凭据 - Registration with Google Credentials In an Android application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM