如何使用 Node js 更新 google 日历事件?
How do I update a google calendar event using Node js?
我正在按照 google 日历文档通过服务帐户 here 更新活动。不幸的是,除了在描述初始设置时考虑到节点(仍然没有通过服务帐户显示过程)之外,google 似乎没有在任何进一步的文档中包含节点风格。
我的身份验证设置如下:
//import google library
const {google} = require("googleapis")
//setup scope of auth
const scopes = ['https://www.googleapis.com/auth/calendar.events'];
//load client secrets from a local file
let credentialsRaw = fs.readFileSync(process.env.GOOGLE_CALENDAR_SECRET);
//get credentials as json parsed from raw file contents
let credentials = JSON.parse(credentialsRaw);
//setup auth obj
let auth = new google.auth.JWT(
credentials.client_email, null,
credentials.private_key, scopes
);
后面是我尝试更新事件的代码:
//setup calendar object
const calendar = await google.calendar({
version: "v3",
auth
});
//make call to update calendar event
let updateResults = await calendar.events.update({
auth: auth,
calendarId: req.body.calendar_id, //log looks like: i798978asdfjka678sagsdfv3344@group.calendar.google.com
eventId: req.body.event_id, //log looks like: 12432dsfkjhhwqejhkj12
resource: {
"summary": "Test Summary",
"description": "Test Description",
"end": req.body.end_time, //log for end and start look like: { dateTime: '2021-08-09T11:30:00-04:00', timeZone: 'America/New_York' }
"start": req.body.start_time,
}
});
我收到的错误是“未找到”。而已。我已经尝试根据 答案使用 encodeURIComponent()
对 calendarId 进行编码,但这并没有改变任何东西。该答案的发布者稍后还提到不再需要编码修复。
我认为我处理正确的帐户方面的一些常见问题:
- 服务帐户所在的项目启用了日历api
- 服务帐户具有全域权限
下面应该向您展示如何授权服务帐户。
const {google} = require('googleapis');
const auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your-secret-key.json',
scopes: ['https://www.googleapis.com/auth/calendar.events'],
});
const service = google.calendar({
version: 'v3',
auth: auth
});
有关详细信息,请参阅 service-account-credentials
Not found
错误通常意味着您无权访问您尝试访问的日历。我会检查以确保代表团已根据日历正确设置。尝试使用 calendar.get 进行测试以确保您可以访问。
我正在按照 google 日历文档通过服务帐户 here 更新活动。不幸的是,除了在描述初始设置时考虑到节点(仍然没有通过服务帐户显示过程)之外,google 似乎没有在任何进一步的文档中包含节点风格。
我的身份验证设置如下:
//import google library
const {google} = require("googleapis")
//setup scope of auth
const scopes = ['https://www.googleapis.com/auth/calendar.events'];
//load client secrets from a local file
let credentialsRaw = fs.readFileSync(process.env.GOOGLE_CALENDAR_SECRET);
//get credentials as json parsed from raw file contents
let credentials = JSON.parse(credentialsRaw);
//setup auth obj
let auth = new google.auth.JWT(
credentials.client_email, null,
credentials.private_key, scopes
);
后面是我尝试更新事件的代码:
//setup calendar object
const calendar = await google.calendar({
version: "v3",
auth
});
//make call to update calendar event
let updateResults = await calendar.events.update({
auth: auth,
calendarId: req.body.calendar_id, //log looks like: i798978asdfjka678sagsdfv3344@group.calendar.google.com
eventId: req.body.event_id, //log looks like: 12432dsfkjhhwqejhkj12
resource: {
"summary": "Test Summary",
"description": "Test Description",
"end": req.body.end_time, //log for end and start look like: { dateTime: '2021-08-09T11:30:00-04:00', timeZone: 'America/New_York' }
"start": req.body.start_time,
}
});
我收到的错误是“未找到”。而已。我已经尝试根据 encodeURIComponent()
对 calendarId 进行编码,但这并没有改变任何东西。该答案的发布者稍后还提到不再需要编码修复。
我认为我处理正确的帐户方面的一些常见问题:
- 服务帐户所在的项目启用了日历api
- 服务帐户具有全域权限
下面应该向您展示如何授权服务帐户。
const {google} = require('googleapis');
const auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your-secret-key.json',
scopes: ['https://www.googleapis.com/auth/calendar.events'],
});
const service = google.calendar({
version: 'v3',
auth: auth
});
有关详细信息,请参阅 service-account-credentials
Not found
错误通常意味着您无权访问您尝试访问的日历。我会检查以确保代表团已根据日历正确设置。尝试使用 calendar.get 进行测试以确保您可以访问。