【问题标题】:Cannot create events in google calendar with service account无法使用服务帐户在 Google 日历中创建活动
【发布时间】:2021-10-31 21:15:14
【问题描述】:

我有两个用于测试的 Google 工作区帐户。账户 A 有一个服务账户,在账户 B 中,我添加了用于域范围委派的服务账户,该账户具有读取/写入账户 B 中日历所需的权限。

阅读事件和日历有效。但是,当尝试使用帐户 B 的资源日历中的服务帐户创建事件时,我收到以下错误:

{"error":{"errors":[{"domain":"calendar","reason":"requiredAccessLevel","message":"You need to have writer access to this calendar."}],"code":403,"message":"You need to have writer access to this calendar."}}

添加的权限:

如果我们在一个 Google Workspace 帐户中进行测试,它就可以工作。

这是代码

// clientMail is the service account id
// privateKey is the service account key
// subject is also the service account id

const jwtClient = new google.auth.JWT(
    clientMail,
    undefined,
    privateKey,
    [
      'https://www.googleapis.com/auth/calendar',
    ],
    subject,
  );

  let calendar = google.calendar({
    version: 'v3',
    auth: jwtClient,
  });
...

savedEvent = (await calendar.events.insert({ calendarId, requestBody: event })).data;
...

【问题讨论】:

  • 请编辑您的问题并包含您的代码。请记住,您需要委托给域上的用户。具体如何完成取决于您使用的编程语言。
  • 感谢您的反馈,我已经这样做了。

标签: node.js google-api google-calendar-api


【解决方案1】:

您似乎忘记指定要将服务帐户委派给哪个用户。

from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

delegated_credentials = credentials.with_subject('user@example.org')

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2020-08-27
    • 2021-12-08
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2022-11-19
    • 2021-09-03
    • 2020-08-11
    相关资源
    最近更新 更多