Friday, 17 March 2017

googleapis TypeError: authClient.request is not a function in nodejs

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);
});

...
...



via user7104874

No comments:

Post a Comment