How to troubleshoot google calendar nodejs api malfunction

I am trying to get information about free classes from Google Calendar using the NodeJS API, as indicated here https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query . The problem is that the only error response I get is an "invalid request". When I run it using the Google Try It tool, I can get a response. I know that I am getting an authorized client. What can I do to fix this problem?

function getAvail(auth, dateTimeRange, calID) { var deferred = Q.defer(); // get a new deferral calendar.freebusy.query({ auth: auth, items: [{id: calID}], timeMin: (dateTimeRange.start).toISOString(), timeMax: (dateTimeRange.end).toISOString(), }, function(err, response) { console.log('Response from the Calendar service: ' + response); if (err) { console.log('There was an error contacting the Calendar service: ' + err); deferred.reject(); // deferred reject here return; } var events = response[calID]['busy']; if (events.length == 0) { console.log('No upcoming events found.'); } else { console.log('busy in here...'); } deferred.resolve(response); // deferred resolve here }); return deferred.promise; // return a promise 

}

+4
source share
1 answer

Phew! Finally, I realized what I was doing wrong. Here is the code that works with comments where I made a mistake. I wish there was a better way to find out this problem.

 function getAvail(auth, dateTimeRange, calID) { console.log('auth:'+JSON.stringify(auth)); console.log('date Time Range :'+(dateTimeRange.start).toISOString()+' --->'+(dateTimeRange.end).toISOString()); console.log('calendar id to check freebusy:'+calID); var deferred = Q.defer(); // get a new deferral calendar.freebusy.query({ auth: auth, headers: { "content-type" : "application/json" }, resource:{items: [{"id" : calID}], //needed to include resource instead of sending the params directly. timeMin: (dateTimeRange.start).toISOString(), timeMax: (dateTimeRange.end).toISOString() } }, function(err, response) { console.log('Response from the Calendar service: ' + JSON.stringify(response)); if (err) { console.log('There was an error contacting the Calendar service: ' + err); deferred.reject(); // deferred reject here return; } var events = response.calendars[calID].busy; if (events.length == 0) { console.log('No upcoming events found.'); } else { console.log('busy in here...'); } deferred.resolve(response); // deferred resolve here }); return deferred.promise; // return a promise } 

There was a lot of thought and looked closely at the documentation. The Google nodejs library is not particularly well documented with examples (well, it's still alpha). But here is the function for frebusy: this.freebusy = {

 /** * calendar.freebusy.query * * @desc Returns free/busy information for a set of calendars. * * @alias calendar.freebusy.query * @memberOf! calendar(v3) * * @param {object} params - Parameters for request * @param {object} params.resource - Request body data** <<<< -- this is what I was missing. * @param {callback} callback - The callback that handles the response. * @return {object} Request object */ query: function(params, callback) { var parameters = { options: { url: 'https://www.googleapis.com/calendar/v3/freeBusy', method: 'POST' }, params: params, requiredParams: [], pathParams: [], context: self }; return createAPIRequest(parameters, callback); } }; 

There was additional knowledge about creating closures inside loops that I learned from SO here: Closing JavaScript inside loops is a simple practical example . Let me know if anyone else encounters similar problems. I will be happy to share my code examples.

+8
source