【问题标题】:How do I update a google calendar event using Node js?如何使用 Node js 更新谷歌日历事件?
【发布时间】:2021-09-30 01:56:23
【问题描述】:

我正在关注谷歌日历文档以通过服务帐户 here 更新活动。不幸的是,除了描述带有节点的初始设置(仍然没有通过服务帐户显示该过程)之外,谷歌似乎没有在任何进一步的文档中包含节点风格。

我的身份验证设置如下:

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

我得到的错误是“未找到”。而已。我已经尝试根据this 的答案用encodeURIComponent() 对calendarId 进行编码,但这并没有改变任何东西。该答案的发布者稍后也提到不再需要编码修复。

账户方面的一些常见问题我认为我已经正确处理了:

  • 服务帐号所在的项目启用了日历 api
  • 服务帐户具有域范围的权限

【问题讨论】:

    标签: javascript node.js google-cloud-platform google-calendar-api


    【解决方案1】:

    以下内容应向您展示如何授权服务帐户。

    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

    找不到

    错误通常意味着您无权访问您尝试访问的日历。我会检查以确保代表团已正确设置到日历中。尝试使用日历进行测试。获取以确保您有访问权限。

    【讨论】:

    • 看起来我的身份验证方法有效,但我没有与我的服务帐户共享正确的日历。我认为域范围内的权威可以做到这一点,但我错了。感谢您解释错误信息!
    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多