Node.js – Google Calendar API showing error "Missing end time" when inserting an event

Issue

I am using Google Calendar library for Node.js to get calendar ID and events list from API. This is working as expected. But when I’m trying to insert or modify an event, I’m facing the common(!) error message “Missing end time”.

I am trying to send a POST request using Request – Simplified HTTP client and not using Google Library or other package.

Here’s my code snippet:

const request = require('request');

// Update Event Title
function insertEventIntoCalendar(calendarId,accessToken){

  let endDateTime = {
        dateTime: '2018-07-03T10:25:00.000-07:00',//end,
        timeZone: 'Asia/Dhaka'
    },
    startDateTime = {
        dateTime: '2018-07-03T10:00:00.000-07:00', //start,
        timeZone: 'Asia/Dhaka'
    },
    url = "https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token="+accessToken,
    options = {
        data: {
            end: endDateTime,
            start: startDateTime,
            summery: 'ARG will win',
            location: '800 Howard St., San Francisco, CA 94103',
            attendees: [],
            reminders: {
                useDefault: true,
            }
        },
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + accessToken,
            'Accept': 'application/json'
        },
        calendarId: 'primary'
    }

  request.post(url, options, function (err, res, body) {
     console.log('err =>', err);
     console.log('body =>', body);
  })
}

And here is my console.log message:

err => null
body => {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Missing end time."
   }
  ],
  "code": 400,
  "message": "Missing end time."
 }
}

Note: I’ve followed all questions and answers related to Google Calendar API and Node.js and still facing the same error. And I’ve not found any answers where Google Library is not used.

Please share your thoughts if you tried this way, otherwise you can suggest me a better way where I don’t need any Config file to get or modify Google Calendar info.

Solution

The problem is in declaring the options variable. The documentation of Request – Simplified HTTP client is saying the json data of Request body should be named as ‘json’. So the options variable should be,

options = {
        url: url
        json: {
            end: endDateTime,
            start: startDateTime,
            summery: 'ARG will win',
            location: '800 Howard St., San Francisco, CA 94103',
            attendees: [],
            reminders: {
                useDefault: true,
            }
        }
    }

and to send the POST request, the code should be:

request.post(options,function (err, res, body) {
        console.log('err =>', err);
        // console.log("res =>", res);
        console.log("body =>", body);
    })

I tried this way and it works 🙂

Answered By – bonnopc

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.