node.js - 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 云功能如果我从活动创建中删除与会者,它运行良好但看不到活动。
谢谢你的帮助
解决方案
推荐阅读
- javascript - 无法使用法国日期格式从给定日期中减去
- python - 如何使用 numpy 有效地查找转换矩阵中的状态变化?
- airwatch - Airwatch Workspace ONE Web 浏览器的功能
- java - 如何为手动依赖注入创建模拟测试
- kubernetes - 在主机文件系统上哪里可以找到 Kubernetes PV?
- jquery - 多个图像滑块不起作用?只有两个图像在滑动,但我需要超过 2 个图像幻灯片?
- javascript - 位置:通过 javascript 添加时相对不起作用
- php - PHP 禁止使用 @ 字符在命令行上不起作用的警告
- excel - 有没有办法命名幻灯片 - 并通过这个名称而不是幻灯片索引来选择幻灯片?
- javascript - 如何在 openlayers v4.6.5 上创建带有矢量的弹出窗口