【问题标题】:Application Default Credentials fails to create the Google App Engine service account credentials应用程序默认凭据无法创建 Google App Engine 服务帐户凭据
【发布时间】:2019-04-26 17:51:35
【问题描述】:

我正在开发一个应用程序,该应用程序使用部署在 AppEngine 上的休息端点启动 Google VM 实例。

在初始化 Compute Engine API 时,我提供了应用程序默认凭据(因为我希望这将返回 AppEngine 服务帐户凭据)

GoogleCredential credential = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
    List<String> scopes = new ArrayList<>();
    // Set Google Compute Engine scope to Read-write.
    scopes.add(ComputeScopes.COMPUTE);
    credential = credential.createScoped(scopes);
}
return new Compute.Builder(httpTransport(), jsonFactory(), credential)
                  .setApplicationName(env.getProperty("spring.application.name"))
                  .build();

但我最终得到了错误:

java.io.IOException: Application Default Credentials failed to create the Google App Engine service account credentials

我正在使用库:

<dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>${google-api-client.version}</version>
</dependency>
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-compute</artifactId>
    <version>${google-api-services-compute.version}</version>
</dependency>

【问题讨论】:

    标签: java google-app-engine google-cloud-platform google-compute-engine


    【解决方案1】:

    我通过使用 google-api-cient-appengine 而不是 google-api-client 解决了这个问题:

    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client-appengine</artifactId>
        <version>${google-api-client-appengine.version}</version>
    </dependency>
    

    并使用 GoogleCredential 初始化:

    GoogleCredential credential = AppEngineCredentialWrapper.getApplicationDefault();
    

    【讨论】:

    • 我需要问一个问题,这些默认凭据存储在哪里?
    • 如果您在 GCP Appengine 中部署您的应用程序,getApplicationDefault 将自动获取链接到 appengine 的服务帐户的凭据。否则,它将尝试解析变量 Environment GOOGLE_APPLICATION_CREDENTIALS,这是包含服务帐户凭据的 json 文件的路径(如果变量未解析,则以错误结尾)
    • 我在本地有一个 java 应用程序,我想向谷歌云进行身份验证,那么该怎么办?
    • 你在使用 appengine devserver 吗?
    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 2021-01-06
    • 2018-05-04
    • 2020-05-27
    • 2016-06-01
    • 2017-10-19
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多