【问题标题】:Creating Google Calendar events with a GCP Service Account使用 GCP 服务帐号创建 Google 日历活动
【发布时间】:2020-08-27 20:21:45
【问题描述】:

我想进行一些操作,以便我的 GCP 服务帐号可以邀请用户参加日历活动。例如,my-service-account@myproject.iam.gserviceaccount.com 将邀请 user@myCorporation.com 参加活动。在我看来,这应该是可能的,只需授予 my-service-account@myproject.iam.gserviceaccount.com 使用日历 API 的权限,而无需 user@myCorporation.com 授予任何其他权限。

我尝试实现此example,但将计算范围和计算 API 调用替换为日历范围和日历 API 调用。我的代码返回错误

Insufficient Permission: Request had insufficient authentication scopes.

我在互联网上翻了一堆,我不知道问题是我做错了什么还是问题是谷歌不支持我正在尝试做的事情。

这是我的代码:

const {google} = require('googleapis');
const compute = google.compute('v1');

const {GoogleAuth} = require('google-auth-library');

async function main() {
  const auth = new GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/calendar',
      'https://www.googleapis.com/auth/compute']
  });

  //keeping the compute stuff in as a sanity check
  //to ensure that the problem is with calendar, not something more general
  const authClient = await auth.getClient();
  const project = await auth.getProjectId();
  const res = await compute.zones.list({project, auth: authClient});
  console.log(res.data);

  createEvent(auth);
}

/**
 * Lists the next 10 events on the user's primary calendar.
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function createEvent(auth) {
  const calendar = google.calendar({version: 'v3', auth});
  calendar.events.insert({
        calendarId: 'primary',
        event: {
          "description": "my test event",
          "start": {
            "date": "2020-05-20",
          },
          attendees: [{email: "myGuest@mailinator.com"}]
        }
      }
  );
}

main().catch(console.error);

【问题讨论】:

  • 你能提供你的净化代码吗?为了创建日历事件,您需要 https://www.googleapis.com/auth/calendar 范围。服务帐户可能很棘手,因此如果没有尽可能多的信息,几乎不可能找到根本原因
  • @RafaGuillermo 感谢您的回复!我用我的代码更新了问题。
  • 这似乎没问题。您是否为与您的服务帐户绑定的 GCP 项目启用了日历 API?此外,在Security > Advanced Settings > Manage API client access 下的admin console 中,确保您已为项目提供了日历范围。如果您不进行委派,请仔细检查您是否也邀请了服务帐户电子邮件到日历,(尽管这不应标记此错误,但值得进行健全性检查)
  • 这可能是问题所在!我是 GCP 项目的管理员,我只是仔细检查了日历 api 是否已启用。但我没有 G Suite 管理员权限。我什至无法访问管理控制台。我需要让 G Suite 管理员授予我的项目权限吗?
  • 你会的,是的。服务帐户将需要在管理控制台中设置的范围,并且只有域管理员才能执行此操作。我在下面添加了这个作为答案

标签: google-calendar-api service-accounts


【解决方案1】:

答案:

您需要在三个位置启用 API 并提供范围:在您的身份验证代码中、在 GCP 控制台中和 Google 管理控制台中。

更多信息:

正如我在 cmets 中所解释的,您提供的代码应该可以正常运行。 Insufficient Permission: Request had insufficient authentication scopes. 错误是由于服务帐户没有被授予访问 Google 一方所需范围的权限。

确保您已完成以下步骤:

  1. 将应用程序中的范围作为身份验证对象提供(您已经这样做了):
const auth = new GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/calendar',
      'https://www.googleapis.com/auth/compute']
  });
  1. GCP console 中为您的服务帐户 GCP 项目启用日历 API。
  2. 在 GCP 的 OAuth 同意屏幕设置中为您的服务帐号提供了所需的范围。
  3. Google Admin console 中的服务帐户添加了所需的范围。这是通过遵循Security > Advanced Settings > Manage API client access UI 元素并将服务帐户所需的所有范围分配给服务帐户客户端 ID 来完成的。

注意:这最后一步必须由域管理员完成,任何非管理员都不能完成。

在这种情况下,您需要联系您的域管理员以授予您的项目 API 访问权限。

参考资料:


相关答案:

【讨论】:

  • 你确定这是准确的吗?我的内部 G Suite 团队说我想要做的事情是不可能的,因为服务帐户没有日历。
  • @bashkirin 完全。服务帐户确实有日历,但由于它们不属于您的域(它们属于 service-acc-project.iam.gserviceaccount.com),如果不使用服务帐户本身,您将无权查看它们。
猜你喜欢
  • 2021-03-29
  • 2021-10-31
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-19
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多