GCP Dialogflow Java-SpringBoot 身份验证 - 应用程序默认凭据不可用

时间:2021-06-18 14:58:50

标签: spring-boot google-cloud-platform dialogflow-es cloud-foundry

我需要连接到 GCP Dialogflow 以检测意图。我正在使用 SpringBoot 和 Java 连接到 Dialogflow。此应用程序在 Pivotal Cloud Foundry (PCF) 中运行,我不知道如何在那里设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。

此处所有建议的解决方案都使用 gcloud、Google SDK 或在 Google Compute Engine 中运行的应用。这些解决方案不适用于我的架构。

GoogleCredentials 始终为 null,即使按照以下建议进行设置 -

GoogleCredentials.fromStream(new FileStream("creds.json"));

GoogleCredentials creds = null;
try {
    creds = GoogleCredentials.fromStream(new FileInputStream("creds.json"));
} catch (Exception e) {
    e.printStackTrace();
}

错误 -

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...

1 个答案:

答案 0 :(得分:1)

经过更多研究,我找到了问题的解决方案。我一直试图在我的 SpringBoot 应用程序中执行此操作。它就在那里,但我之前第一次遇到它时并没有多想。

PCF 中的环境变量可以通过 cf CLI 设置。我所要做的就是在管道中添加一个额外的命令,而且效果很好 -

cf set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE

来源 - https://cli.cloudfoundry.org/en-US/v6/set-env.html https://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html

附言如果有新应用被推送到 PCF,请在运行上述命令之前进行验证,因为在这种情况下不会找到 APP_NAME。