1

I am using googleapis and google auth for google calendar. I have fetch calendar event list but when i try to insert event then it throw an error i.e, TypeError: authClient.request is not a function in nodejs.

Here is my code snippet:-

//route.js

...
app.get('/auth/google', passport.authenticate('google', {
        scope: ['profile', 'email', 'https://www.googleapis.com/auth/calendar']
    }));
...

//server/passport.js

... 
var google = require('googleapis');
var googleAuth = require('google-auth-library');
...
 passport.use(new GoogleStrategy({
            clientID: configAuth.googleAuth.clientID,
            clientSecret: configAuth.googleAuth.clientSecret,
            callbackURL: configAuth.googleAuth.callbackURL,
            passReqToCallback: true
        },
        function(req, token, refreshToken, profile, done) {
...
var clientSecret = configAuth.googleAuth.clientSecret;
var clientId = configAuth.googleAuth.clientID;
var redirectUrl = configAuth.googleAuth.callbackURL;
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
var tokenObj = { access_token: token, refresh_token: refreshToken }
                            oauth2Client.credentials = tokenObj;

req.session.oauth2Client = oauth2Client;


var calendar = google.calendar('v3');
 calendar.events.list({
     auth: oauth2Client,
     calendarId: 'primary',
     timeMin: (new Date()).toISOString(),
     maxResults: 10,
     singleEvents: true,
     orderBy: 'startTime'
  }, function(err, response) {
     if (err) {
         console.log('The API returned an error: ' + err)
         return;
     }
     var events = response.items;
     var googleEvents = [];
     if (events.length == 0) {
        console.log('No upcoming events found.');
    } else {
        for (var i = 0; i < events.length; i++) {
           var temp = {};
           temp.info = events[i].summary;
           temp.start = events[i].start.dateTime || events[i].start.date;
           googleEvents.push(temp);
         }
     }
     console.log(googleEvents); // it is work fine
 });
...
...
}))

//server/controller/todo.js

....
....
var event = {
                'summary': 'Google I/O 2015',
                'location': '800 Howard St., San Francisco, CA 94103',
                'description': 'A chance to hear more about Google\'s developer products.',
                'start': {
                    'dateTime': '2017-03-20T09:00:00-07:00',
                    'timeZone': 'America/Los_Angeles',
                },
                'end': {
                    'dateTime': '2017-03-23T17:00:00-07:00',
                    'timeZone': 'America/Los_Angeles',
                },
                'recurrence': [
                    'RRULE:FREQ=DAILY;COUNT=2'
                ],
                'reminders': {
                    'useDefault': false,
                    'overrides': [
                        { 'method': 'email', 'minutes': 24 * 60 },
                        { 'method': 'popup', 'minutes': 10 },
                    ],
                },
            };


var calendar = google.calendar('v3');
calendar.events.insert({     //here ERROR 'TypeError: authClient.request is not a function' is  generated
    auth: req.session.oauth2Client,
    calendarId: 'primary',
    resource: event,
}, function(err, event) {
     if (err) {
         console.log('There was an error contacting the Calendar service: ' + err);
      return;
   }
   console.log('Event created: %s', event.htmlLink);
});

...
...
user7104874
  • 1,321
  • 2
  • 15
  • 21
  • You may refer with this [thread](http://stackoverflow.com/questions/39621428). Maybe the problem is with `oauth_client`. You may also follow this [documentation](https://github.com/google/google-auth-library-nodejs) to see if you missed something with your configuration. Be noted that before making your API call, you must be sure the API you're calling has been enabled. Go to `APIs & Auth` > `APIs` in the [Google Developers Console](https://console.developer.google.com/) and enable the APIs you'd like to call. For the example below, you must enable the DNS API. – abielita Mar 18 '17 at 06:28

1 Answers1

3

This doesn't work because req.session is stored as json:

To store or access session data, simply use the request property req.session, which is (generally) serialized as JSON by the store, so nested objects are typically fine. https://github.com/expressjs/session

When being stored as json, all methods are stripped from the object and only the data is saved.

I have solved it by creating a seperate, global object containing all oauth objects, retrievable by the session id:

//Global object
var auths = {};

//...

//Save Client
auths[req.session.id] = oauth2Client;

//...

//use client
var calendar = google.calendar('v3');
calendar.events.insert({    
  auth: auths[req.session.id],
  calendarId: 'primary',
Josef Hoppe
  • 376
  • 2
  • 9