使用服务帐户将 google 日历添加到特定用户 calendarList

Add google calendar to specific users calendarList using a service account

我正在使用服务帐户在我们的系统中创建新日历。

async function newCalendar (email) {
  console.log('creating calendar for username', email, 'as', config.google.calendar.clientEmail);
  const auth = await getAuth();
  const calendar = google.calendar({version: 'v3', auth});
  const newCal = await calendar.calendars.insert({
    resource: {summary: `SYSCALENDAR(${email})`}});
  const calendarId = newCal.data.id;
  const associated = await calendar.acl.insert({
    calendarId,
    requestBody: {
      role: 'reader',
      scope: {
        type: 'user',
        value: email
      }
    },
    sendNotifications: false
  });
  return calendarId;
}

如上所示,日历创建为执行 calendars.insert 调用的服务帐户所拥有。

我还注入了一条 calendar.acl 记录,我认为它授予 'email' 标识的用户访问此日历的权限。

在这段代码中,我有:

sendNotifications: false

如果我将其设置为 true,用户会收到一封关于新 ACL 条目的电子邮件,并且可以单击以将日历添加到他们自己的日历列表中。

我不希望用户必须这样做,而是想在代码中将日历添加到日历列表中,作为服务帐户。

这不像 calendarList.insert(calendarId) 那样简单,因为它将日历插入到服务帐户日历列表中。

最近 Google 处理由服务帐户创建/修改的日历的方式发生了变化。

如果没有用户的手动批准,将无法再将用户添加到此类日历。

绕过它(仅适用于域用户)的唯一方法是Perform G Suite Domain-Wide Delegation of Authority。基本上,让服务帐户代表用户工作并让服务帐户代表用户接受邀请。