I took the code from here: https://developers.google.com/google-apps/calendar/quickstart/nodejs and got it working locally and to return a JSON object of the calendar events.
I turned it into a module called GoogleCalendarAPI.js, and still had it working locally.
I tried the approach from this StackOverflow answer: How to oAuth Google API from Lambda AWS?. I'm getting the same result whether I use the Token, retrieve as described there; or whether I use the client_secret.json file.
exports.getCalendarJSONEvents =
function getCalendarJSONEvents(callback) {
console.log("started getCalendarJSONEvents");
listEvents(TOKEN, function(jsonResult) {
console.log("Json Callback Events=");
console.log(jsonResult);
callback(jsonResult);
});
}
function listEvents(auth, callback) {... etc...
The calling Lambda index.js does this:
console.log("log: WhenIntent");
var GoogleCalendarAPI = require('./GoogleCalendarAPI.js');
GoogleCalendarAPI.getCalendarJSONEvents(
function callback(calEvents){
console.log("WRAPPER: calendarResults=" +
JSON.stringify(calEvents));
// chunk misc code here
this.emit(':tell', speechText);
});
The last thing I see in my log is "log: WhenIntent". It's an Alexa App, and I'm using my user account to access one public calendar. I have no idea what it's doing after that, and not sure how to debug it any further. I'm not sure why I don't see "started getCalendarJSONEvents" before the timeout (default 3 secsond, and also tried 5 seconds).
Update 1:
Okay, the CloudWatch Log did show "The API returned an error: Error: Bad Request", and I thought it was Lambda error, but it was coming from the listEvents. I added line noted below, and at least now the logic flowed back, and I will the reason for the "Bad Request" later.
function listEvents(auth, callback) {
console.log("log: start listEvents");
var calendar = google.calendar('v3');
console.log("log: listEvents got calendar var");
var jsonOut = "{";
calendar.events.list({
auth: auth,
calendarId: 'somename@group.calendar.google.com',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
// made this error more clear!
console.log('The Google Calendar API returned an error: ' + err);
callback(err); // added this line
return;
}
var events = response.items;
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
console.log('Upcoming 10 events:');
var jsonLine = "";
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);
jsonLine =
[{ 'date': event.start.date,
'summary': event.summary
}];
console.log("jsonline=")
jsonOut = jsonOut + JSON.stringify(jsonLine) + ",";
}
jsonOut = jsonOut + "}";
}
console.log("End of listEvents: jsonOut = " +
JSON.stringify(jsonOut));
callback(jsonOut);
});