【问题标题】:How can I have a service account impersonate another service account? (Node.js)如何让服务帐户模拟另一个服务帐户? (Node.js)
【发布时间】:2020-05-31 10:17:37
【问题描述】:

我通过将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为我的 App Engine 服务帐户(例如 example@appspot.gserviceaccount.com)的密钥路径来进行身份验证。

如果日历直接共享到 App Engine 服务帐户,我可以这样做:

let googleCalendar = google.calendar({
  version: 'v3',
  auth: new google.auth.GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/calendar'],
  })
});

但我希望用户与我拥有的另一个服务帐户google-calendar@example.iam.gserviceaccount.com 共享他们的日历。所以我希望 App Engine 服务帐户模拟 google-calendar 帐户。我已经尝试过这个(以及其他一些小的变化):

let googleCalendar = google.calendar({
  version: 'v3',
  auth: new google.auth.GoogleAuth({
    clientOptions: {
      subject: 'google-calendar@uniserval-app.iam.gserviceaccount.com'
    },
    scopes: ['https://www.googleapis.com/auth/calendar'],
  })
});

我收到 401 错误:Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.

我尝试让我的 App Engine 服务帐户成为 google-calendar 服务帐户的成员,并为其授予各种角色,例如 Service Account User 和 Service Account Token Creator - 但没有任何变化。

我怀疑代码是正确的,我只是没有配置正确的角色...但是在这一点上,我已经搜索了 2 天如何执行此操作,但我找不到任何关于如何执行此操作的文档这样做。

【问题讨论】:

  • 仔细检查您是否已将角色 roles/iam.serviceAccountUser 分配给分配给 App Engine 的服务帐户。错误消息表明您没有正确执行此操作。接下来,您为允许访问日历的google-calendar 服务帐户分配了哪些角色?
  • 这是我分配角色的方式:i.imgur.com/QiKyENw.png 至于google-calendar 如何访问日历,是因为在 Google 日历中,我与该服务帐户电子邮件共享日历。但这部分不是问题,而是有效的。
  • 为什么不向 App Engine 服务帐户授予日历权限,而不是尝试模拟服务帐户?
  • #1 原因是因为我已经拥有与该服务帐户共享的多个帐户的日历。在我的情况下,让这些用户编辑他们日历的权限并不难——但如果它已经被使用了数百次呢?主要是我只想知道怎么做,因为文档说这是可能的。我之前的做法是从我的google-calendar 帐户中获得一个secret.json,然后将其部署到我的应用程序中。让我的 App Engine 服务帐户能够模拟第二个服务帐户看起来更干净。
  • 实际上,将具有正确日历权限的服务帐户分配给 App Engine 更清洁。但是,您有两个我提到的导致此问题的区域。一个或两个都是你的问题。

标签: google-cloud-platform google-api-nodejs-client


【解决方案1】:

您的语法看起来是正确的,只是您似乎误解了将哪个电子邮件地址设置为主题。主题是您希望代表其执行操作的用户帐户。

new google.auth.GoogleAuth({
  clientOptions: {
    subject: 'someone@yourdomain.com'
  },
  scopes: ['https://www.googleapis.com/auth/calendar'],
})

【讨论】:

    猜你喜欢
    • 2022-07-28
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2015-01-03
    • 2014-01-07
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多