Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 谷歌日历API节点JS,可以';无法使用服务帐户添加与会者_Node.js_Google Cloud Functions_Google Calendar Api_Service Accounts - Fatal编程技术网

Node.js 谷歌日历API节点JS,可以';无法使用服务帐户添加与会者

Node.js 谷歌日历API节点JS,可以';无法使用服务帐户添加与会者,node.js,google-cloud-functions,google-calendar-api,service-accounts,Node.js,Google Cloud Functions,Google Calendar Api,Service Accounts,我目前正在尝试开发一个部署在GCP云函数上的简单Node JS程序,将Google日历API与服务帐户结合使用 程序很简单,我只想创建一个事件并添加与会者 我的代码运行良好,但是我无法使用服务帐户添加与会者。我有以下错误: There was an error contacting the Calendar service: Error: Service accounts cannot invite attendees without Domain-Wide Delegation of Auth

我目前正在尝试开发一个部署在GCP云函数上的简单Node JS程序,将Google日历API与服务帐户结合使用

程序很简单,我只想创建一个事件并添加与会者

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

There was an error contacting the Calendar service: Error: Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.
API被激活,我允许GSuite管理员安全上的日历范围,我的服务帐户在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云功能 如果我将与会者从活动创建中删除,它可以正常工作,但无法查看活动


感谢您的帮助

设置域范围的授权

  • 访问您的
  • 转到
    IAM&Admin
    ->服务帐户
  • 选择您在脚本中使用的服务帐户
  • 选中
    启用G套件域范围的委派
如果您确定已为正确的服务帐户启用了域范围的委派,并且仅在与与会者一起创建活动时才会遇到问题,请执行以下操作: 您的问题可能与Google的问题跟踪程序上报告的问题有关:


目前,对于使用服务帐户与与会者一起创建活动存在限制,尤其是当与会者不在您的域内时。

当您创建
jwtClient
时,我认为您必须将用户主电子邮件发送到
JWT
方法

比如:


感谢您的回答。但是,我已经启用了G Suite域范围的委派,错误仍然存在。如上所述,您的问题可能与使用已报告的服务帐户创建与会者活动的一般问题有关。许多其他人也经历过同样的问题-例如,参见或。这是模仿,怎么回事?他们在这个问题上犯了完全不同的错误。
let jwtClient = new google.auth.JWT(
           privatekey.client_email,
           null,
           privatekey.private_key,
           SCOPES,
           "yourUserPrimaryEmail"); // here