0

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); 
  });
NealWalters
  • 17,197
  • 42
  • 141
  • 251
  • Do you get an error like ‘read/write on closed pipe’? – nilobarp Dec 01 '17 at 22:59
  • It is possible that `require('./GoogleCalendarAPI.js')` is not able to find the file and has throw an exception. Can you add `console.log` after require and try? Can you add log output from AWS Lambda here? – RC_02 Dec 01 '17 at 23:11
  • Thanks, see my update, think I figured it out. When I use the TOKEN, I get bad request. I'll have to switch back to the "client_secret.json" to see what happens with that one. – NealWalters Dec 01 '17 at 23:23
  • Timeout seems to be gone now even the other way; still can't read the calendar, but that's an OAUTH issue. – NealWalters Dec 01 '17 at 23:30
  • @nilobarp Not related to this question, but what causes read/write on closed pipe errors? I'm getting that issue from functions that appear to be returning correctly (at least sam local shows the report for the requestID) – Mars Feb 20 '18 at 01:21
  • @Mars 99% of time it is the execution timeout. This is what happened to me https://stackoverflow.com/q/47089665/1790045 – nilobarp Feb 20 '18 at 03:20
  • @nilobarp Ah, I thought I recognized your name from somewhere! Unfortunately, in my case the function runs and returns in .1 seconds with a 90 second timeout, but still gives off the pipe error... – Mars Feb 20 '18 at 04:02

1 Answers1

0

If your timeout error is ETIMEDOUT and your lambda belongs to the VPC check the VPC can connect to the internet.

if not

create a subnet in relevant VPC create a NAT Gateway attached with subnet