簡體   English   中英

Google Dialogflow:應用程序默認憑據不可用

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

嗨,我對 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 設置為 Application Server 的環境變量。

與問題中提到的不同,以這種方式提供時,我已經獲得了“憑證”

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

但是與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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM