简体   繁体   English

使用带有 NodeJS 的 Google 日历 API 将事件添加到 Google 日历

[英]Add event to Google Calendar using Google Calendar API with NodeJS

We are trying to add an event to Google Calendar using Google Calendar API with NodeJS, we created the service account, we have set the delegation on g suite on the service account, we invited the service account email to the calendar, we added the calendar api to the project and we added the role to the impersonification user to create the tokens and we did everything we hav read following this guide:我们正在尝试使用带有 NodeJS 的 Google 日历 API 将事件添加到 Google 日历,我们创建了服务帐户,我们在服务帐户的 g 套件上设置了委托,我们邀请服务帐户 email 到日历,我们添加了日历api 到项目,我们将角色添加到模拟用户以创建令牌,我们按照本指南完成了我们阅读的所有内容:

Google Calendar API Service Account Error Google 日历 API 服务帐户错误

The only step we could not achieve was to add the delegation to the impersonification user because there is no button or switch to add it (but the service account user has the delecation active).我们无法实现的唯一步骤是将委托添加到模拟用户,因为没有按钮或开关来添加它(但服务帐户用户激活了委托)。

The code we are using is:我们使用的代码是:

 async function generateMeet(discipline, { from, to }, { customer1, customer2 }) {
  const SCOPES = [
   'https://www.googleapis.com/auth/calendar',
   'https://www.googleapis.com/auth/calendar.events',
   'https://www.googleapis.com/auth/admin.directory.resource.calendar',
  ]

  const jwtClient = new google.auth.JWT(
    credentials.client_email,
    null,
    credentials.private_key,
    SCOPES,
    //we also tried to add the impersonification subject, but the authentication fails.
  )

  try {
    //authenticate request
    await jwtClient.authorize()
  } catch (e) {
    console.error('Error during google authenticaion', e)
    throw new Error(e)
  }

  const calendar = google.calendar({ version: 'v3', auth: jwtClient })
  const requestId = crypto.randomBytes(64).toString('hex')
  const event = {
    summary: `Title with ${discipline.label_it}`,
    description: 'Example.',
    start: {
      dateTime: from,
      timeZone: 'Europe/Rome',
    },
    end: {
      dateTime: to,
      timeZone: 'Europe/Rome',
    },
    conferenceData: {
      createRequest: { requestId },
    },
    attendees: [{ email: customer1 }, { email: customer2 }],
    reminders: {
      useDefault: false,
      overrides: [
        { method: 'email', minutes: 24 * 60 },
        { method: 'popup', minutes: 30 },
      ],
    },
  }

  return calendar.events.insert({
    auth: jwtClient,
    calendarId: process.env.MY_GOOGLE_CALENDAR_ID,
    resource: event,
  })
}

We receive this error:我们收到此错误:

code: 403,
errors: [
    {
      domain: 'calendar',
      reason: 'forbiddenForServiceAccounts',
      message: 'Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.'
    }
  ]

Answer回答

You have to make some extra steps as the Identity Platform documentation says Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators .您必须按照Identity Platform 文档所述执行一些额外步骤Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators

In order to be granted you have to follow the steps as in the previous link.为了获得授权,您必须按照上一个链接中的步骤操作。

Bear in mind:记住:

  • It usually takes a few minutes for impersonation access to be granted after the client ID was added, but in some cases, it might take up to 24 hours to propagate to all users of your Google Account.

Reference参考

Delegating domain-wide authority to the service account 将域范围的权限委托给服务帐户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM