I am trying to get free-busy information from Google Calendar using the NodeJS API as listed here https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query. The problem is that the only error response I get is 'invalid request'. When I run it with the Google's Try It tool, i am able to get response though. I know that I am getting an authorized client. What can I do to troubleshoot this further?
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
}
Enable API accessIn the API Manager pane, click Library to see the list of available APIs. In the Google Apps APIs list, scroll down to click or search for Calendar API, then on the API page, click Enable. Go back to the Library.
The Google Calendar API is a RESTful API that can be accessed through explicit HTTP calls or via the Google Client Libraries.
Phew! I finally figured out what I was doing wrong. Here is the code that works with comments where I was making a mistake. I wish there was a better way to finding out this issue.
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
}
It was a lot of meditation and staring at the documentation closely. Google's nodejs library is not particularly well-documented with examples yet (well, it is still alpha). But here is the function for freebusy:
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 were some additional learns about creating closures inside loops which I learned on SO from here: JavaScript closure inside loops – simple practical example. Let me know if anyone else runs into the similar issues. I will be happy to share my code samples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With