Reputation: 266
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
}
Upvotes: 5
Views: 2802
Reputation: 266
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.
Upvotes: 11