google-api-nodejs-client: Calendar API gives an error while inserting events using events.insert

Hi, I used the code sample in Quickstart and tried inserting an event using the example on - https://developers.google.com/calendar/v3/reference/events/insert#examples

I checked the resources available over the web and none of them seem to be working for me (downgrading googleapis package to version 24.0.0 or using different ways of writing the insert event). This is the code of the addEvents function currently -

function addEvents(auth){

var event = {
  "summary": "Google I/O 2015",
  "location": "800 Howard St., San Francisco, CA 94103",
  "description": "A chance to hear more about Google\'s developer products.",
  "start": {
    "dateTime": "2015-05-28T09:00:00-07:00",
    "timeZone": "America/Los_Angeles",
  },
  "end": {
    "dateTime": "2015-05-28T17:00:00-07:00",
    "timeZone": "America/Los_Angeles",
  },
  "recurrence": [
    "RRULE:FREQ=DAILY;COUNT=2"
  ],
  "reminders": {
    "useDefault": false,
    "overrides": [
      {"method": "email", "minutes": 24 * 60},
      {"method": "popup", "minutes": 10},
    ],
  },
};

//console.log(event)

  var calendar = google.calendar("v3");

  calendar.events.insert({
  auth: auth,
  calendarId: "primary",
  resource: event,
}, function(err, event) {
  if (err) {
    console.log("There was an error contacting the Calendar service: " + err);
    return;
  }
  console.log("Event created: %s", event.htmlLink);
});

}

It is on the same lines as the listEvents function -

function listEvents(auth) {
var calendar = google.calendar('v3');
calendar.events.list({
    auth: auth,
    calendarId: 'primary',
    timeMin: (new Date()).toISOString(),
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime'
    },
    function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    var events = response.items;
    if (events.length == 0) {
      console.log('No upcoming events found.');
    }
    else {
      console.log('Upcoming 10 events:');
      for (var i = 0; i < events.length; i++) {
        var event = events[i];
        var start = event.start.dateTime || event.start.date;
        console.log('%s - %s', start, event.summary);
    }
    }
  });
}

Can anyone please help me with this?

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Greetings! Sadly the docs on developers.google.com are out of date; we are working to get them fixed. In the interim - here are a few changes you need to make:

  1. Make sure you didn’t npm install google-auth-library. If you did, delete it from your package.json, and run npm install again.

  2. Import the library using const {google} = require('googleapis');, notice the {} around google.

  3. In your callback, instead of looking for response.items, use response.data.items. This will be required in any of the callbacks coming from the API.

I am guessing that after doing these things, you’ll be back on rails 😃 Let me know if you run into any problems!