首页 > 解决方案 > Google Calendar API Node JS,无法使用服务帐户添加与会者

问题描述

我目前正在尝试开发一个部署在 GCP Cloud Function 上的简单 Node JS 程序,以将 Google Calendar API 与服务帐户一起使用。

该程序很简单,我只想创建一个活动并添加参与者。

我的代码运行良好,但是我无法使用服务帐户添加与会者我有这个错误:

There was an error contacting the Calendar service: Error: Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.

API 已激活,我允许 GSuite Admin Security 上的日历范围,我的服务帐户在 GCP 上检查了委派权限。

我搜索了很多解决方案,但没有解决我的问题..

这是我的模块的代码:

const { google } = require('googleapis');
const sheets = google.sheets('v4');

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/calendar'];
let privatekey = require("../creds.json");

async function getAuthToken() {
    let jwtClient = new google.auth.JWT(
           privatekey.client_email,
           null,
           privatekey.private_key,
           SCOPES);
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
     if (err) {
       console.log(err);
       return;
     } else {
       console.log("Successfully connected!");
     }
    });
    return jwtClient;
}


async function insertEvents(auth,items) {

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

  var startDate = new Date();
  var endDate = new Date();
  startDate.setHours(14, 0, 0, 0);
  endDate.setHours(15, 0, 0, 0);

    var attendees = [];

    items.forEach(function(item){
       attendees.push({email: item[2]});
    });


  var event = {
    summary: 'Stack Roulette : It\'s Coffee Time =)',
    location: 'Partout, mais avec un café',
    description: "",
    start: {
      dateTime: startDate.toISOString(),
      timeZone: 'Europe/London'
    },
    end: {
      dateTime: endDate.toISOString(),
      timeZone: 'Europe/London'
    },
   attendees: attendees,
    reminders: {
      useDefault: false,
      overrides: [
        { method: 'email', minutes: 24 * 60 },
        { method: 'popup', minutes: 10 }
      ]
    },
    conferenceData: {
    createRequest: {requestId: Math.random().toString(36).substring(2, 10),
      conferenceSolution: {
        key: {
            type: "hangoutsMeet"
        }
      },}
  }
  };



  calendar.events.insert(
    {
      auth: auth,
      calendarId: 'primary',
      resource: event,
      conferenceDataVersion: 1
    },
    function(err, event) {
      if (err) {
        console.log(
          'There was an error contacting the Calendar service: ' + err
        );
        return;
      }
      console.log('Event created: %s', event.data.htmlLink);
    }
  );
}

module.exports = {
  getAuthToken,
  getSpreadSheetValues,
  insertEvents
}

准确地说,我的应用程序没有前端,代码使用云函数(如 API 端点)运行。

PS:它不是 Firebase 云功能,而是 GCP 云功能如果我从活动创建中删除与会者,它运行良好但看不到活动。

谢谢你的帮助

标签: node.jsgoogle-cloud-functionsgoogle-calendar-apiservice-accounts

解决方案


设置域范围的授权

  • 访问您的云控制台
  • 转到IAM & Admin-> 服务帐户
  • 选择您在脚本中使用的服务帐户
  • 查看Enable G Suite Domain-wide Delegation

如果您确定您已经为正确的服务帐户启用了域范围的委派,并且仅在与与会者一起创建活动时遇到问题:

您的问题可能与Google 问题跟踪器上报告的此问题有关:

目前,与具有服务帐户的与会者一起创建活动存在限制,尤其是当与会者不在您的域中时。


推荐阅读