简体   繁体   English

如何使用 Node js 更新谷歌日历事件?

[英]How do I update a google calendar event using Node js?

I'm following the google calendar documentation to update an event via a service account here .我正在按照 google 日历文档通过此处的服务帐户更新事件。 Unfortunately, besides describing the initial setup with node in mind (which still doesn't show the process via a service account) google doesn't seem to include the node flavor in any further documentation.不幸的是,除了在考虑节点的情况下描述初始设置(它仍然没有通过服务帐户显示过程)之外,谷歌似乎没有在任何进一步的文档中包含节点风格。

My authentication is setup below:我的身份验证设置如下:

//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
);

Followed by my code that tries to update the event:其次是我尝试更新事件的代码:

//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,
    }
});

The error I get is "Not found".我得到的错误是“未找到”。 That's it.而已。 I've tried encoding the calendarId with encodeURIComponent() according to this answer but that didn't change anything.我已经尝试根据这个答案使用encodeURIComponent()对 calendarId 进行编码,但这并没有改变任何东西。 The poster of that answer also mentioned later that the encoding fix isn't needed anymore.该答案的发布者稍后还提到不再需要编码修复。

Some common issues on the account side I think I've handled correctly:账户方面的一些常见问题我认为我已经正确处理了:

  • The project that the service account is in has the calendar api enabled服务帐号所在的项目启用了日历api
  • The service account has domain wide authority服务帐户具有域范围的权限

The following should show you how to authorize the service account.下面应该向您展示如何授权服务帐户。

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 
});

For more info see service-account-credentials有关更多信息,请参阅service-account-credentials

Not found未找到

Error normally means that you do not have access to the calendar you are trying to access.错误通常意味着您无权访问您尝试访问的日历。 I would check to be sure the delegation was setup properly to the calendar.我会检查以确保代表团按照日历正确设置。 Try testing with a calendar.get to be sure you have access.尝试使用 calendar.get 进行测试以确保您可以访问。

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

相关问题 如何使用 javascript 更新 Google 日历中的事件? - How to update an event in Google Calendar using javascript? 在节点js中获取Google日历事件ID - Get google calendar event ID in node js 如何使用 Google Apps 脚本通过给定的 Meet URL 创建/更改 Google 日历活动? - How do I create/change a Google Calendar Event with a given Meet URL using Google Apps Script? 为什么 Google Calendar API 会比我使用 Node.js 提供的日期/时间早 4 小时插入我的活动? - Why does Google Calendar API insert my event 4 hours earlier than the date/time that I've given it using Node.js? Google Calendar API-如何使用JavaScript创建QUICK ADD事件? - Google Calendar API - How do I create a QUICK ADD event using javascript? 使用Javascript和Google Calendar API v3,如何允许用户指定非主日历来添加事件? - Using Javascript & Google Calendar API v3, how do I allow the user to specify a non-primary calendar to add an event? 如何从Google日历活动中获取Google文档/文档网址? - How do I get Google Docs/atachment URL from a Google Calendar event? 如何使用节点js在mysql中使用数组更新多行 - How do I update multiple rows using an array in mysql using node js 使用 Node.js 创建重复的 Outlook 日历事件 - Create Recurring Outlook Calendar Event using Node.js 如何等待Node.js中的下一个事件? - How do I wait for the next event in Node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM