【问题标题】:Authenticate service account without downloaded key on google app engine在谷歌应用引擎上验证服务帐户而不下载密钥
【发布时间】:2017-09-24 17:25:46
【问题描述】:

我正在开发一个应该安装在 Google App Engine 中的产品。

在此我使用服务帐户来验证 Gmail API、Drive API、日历 API 等。

下载的 P12 文件作为身份验证工作正常。但作为它的产品,我不希望客户端在每次安装时都下载并上传到应用程序上。

有没有一种方法可以在没有私钥文件的情况下对其进行身份验证,或者在没有服务帐户的情况下使用该 API。

在下面的页面中提到,系统管理的密钥对由 Google 自动管理。有帮助吗?我没有找到任何例子。

https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts.keys

在下面的链接中,它建议对于 Google Cloud Platform 我应该使用 Google Managed Key https://cloud.google.com/iam/docs/understanding-service-accounts

这个密钥可以在没有下载文件的情况下使用吗?

谢谢

【问题讨论】:

  • 你不必使用p12文件也可以使用json keyfile。
  • @DaImTo 感谢您的评论。它知道可以使用任何一个文件,但它不会解决问题。我只是不希望用户在应用程序中创建密钥并上传文件。至少如果有办法使用 API 直接在服务器上创建密钥和下载文件。会有帮助的。

标签: google-app-engine google-api google-api-java-client google-authentication


【解决方案1】:

我可以通过 IAM API 来实现 https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts.keys

下面是它的Java代码

AppIdentityCredential credential = new AppIdentityCredential(
                Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
Iam iam = new Iam(httpTRANSPORT, jsonFACTORY, credential);
try {
    Iam.Projects.ServiceAccounts.Keys.Create keyCreate = iam.projects().serviceAccounts().keys()
                    .create("projects/myProject/serviceAccounts/myProject@appspot.gserviceaccount.com", new CreateServiceAccountKeyRequest());

    ServiceAccountKey key = keyCreate.execute();

} catch (IOException e) {
    System.out.println(e.getMessage());
}

任何密钥都可用于生成如下 GoogleCredential

InputStream stream = new ByteArrayInputStream(key.decodePrivateKeyData());
GoogleCredential credential = GoogleCredential.fromStream(stream);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2018-11-10
    • 2013-07-05
    • 2012-03-22
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多