简体   繁体   中英

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 . 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. 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
  • 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM