Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternalOAuthError: Failed to obtain request token passport-google in nodejs

I am trying to authenticate user, using google authentication passport-google but it keeps sending InternalOAuthError: Failed to obtain request token.

error view

InternalOAuthError: Failed to obtain request token at Strategy.OAuthStrategy._createOAuthError (/Users/menaka/WebstormProjects/cardCreaterServer/node_modules/passport-oauth1/lib/strategy.js:396:17) at /Users/menaka/WebstormProjects/cardCreaterServer/node_modules/passport-oauth1/lib/strategy.js:244:41 at /Users/menaka/WebstormProjects/cardCreaterServer/node_modules/oauth/lib/oauth.js:543:17 at passBackControl (/Users/menaka/WebstormProjects/cardCreaterServer/node_modules/oauth/lib/oauth.js:397:13) at IncomingMessage. (/Users/menaka/WebstormProjects/cardCreaterServer/node_modules/oauth/lib/oauth.js:409:9) at emitNone (events.js:72:20) at IncomingMessage.emit (events.js:166:7) at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9) at process._tickCallback (node.js:356:17)

I have already enabled Google + api inside the google API Manager platform.and here is my Authorized JavaScript origins

http://localhost:3000

and Authorized redirect URIs

http://localhost:3000/auth/google/callback

in side my routes.js file

var User = require('./models/user');
module.exports = function(app, passport){
    app.get('/', function(req, res){
        res.render('index.ejs');
    });

    app.get('/login', function(req, res){
        res.render('login.ejs', { message: req.flash('loginMessage') });
    });
    app.post('/login', passport.authenticate('local-login', {
        successRedirect: '/profile',
        failureRedirect: '/login',
        failureFlash: true
    }));

    app.get('/signup', function(req, res){
        res.render('signup.ejs', { message: req.flash('signupMessage') });
    });


    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect: '/',
        failureRedirect: '/signup',
        failureFlash: true
    }));

    app.get('/profile', isLoggedIn, function(req, res){
        res.render('profile.ejs', { user: req.user });
    });

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

    app.get('/auth/google/callback',
        passport.authenticate('google', { successRedirect: '/profile',
            failureRedirect: '/' }));


    app.get('/logout', function(req, res){
        req.logout();
        res.redirect('/');
    })
};

function isLoggedIn(req, res, next) {
    if(req.isAuthenticated()){
        return next();
    }

    res.redirect('/login');
}

inside my passport.js

var LocalStrategy = require('passport-local').Strategy;

var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;

var User            = require('../app/models/user');

var configAuth=require('./auth');

module.exports = function(passport) {


    passport.serializeUser(function(user, done){
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done){
        User.findById(id, function(err, user){
            done(err, user);
        });
    });


    passport.use('local-signup', new LocalStrategy({
            usernameField: 'email',
            passwordField: 'password',
            passReqToCallback: true
        },
        function(req, email, password, done){
            process.nextTick(function(){
                User.findOne({'local.username': email}, function(err, user){
                    if(err)
                        return done(err);
                    if(user){
                        return done(null, false, req.flash('signupMessage', 'That email already taken'));
                    } else {
                        var newUser = new User();
                        newUser.local.username = email;
                        newUser.local.password = newUser.generateHash(password);

                        newUser.save(function(err){
                            if(err)
                                throw err;
                            return done(null, newUser);
                        })
                    }
                })

            });
        }));

    passport.use('local-login', new LocalStrategy({
            usernameField: 'email',
            passwordField: 'password',
            passReqToCallback: true
        },
        function(req, email, password, done){
            process.nextTick(function(){
                User.findOne({ 'local.username': email}, function(err, user){
                    if(err)
                        return done(err);
                    if(!user)
                        return done(null, false, req.flash('loginMessage', 'No User found'));
                    if(!user.validPassword(password)){
                        return done(null, false, req.flash('loginMessage', 'invalid password'));
                    }
                    return done(null, user);

                });
            });
        }
    ));

    passport.use(new GoogleStrategy({
            consumerKey: configAuth.GoogleAuth.clientID,
            consumerSecret: configAuth.GoogleAuth.clientSecret,
            callbackURL: configAuth.GoogleAuth.callbackURL
        },
        function(accessToken, refreshToken, profile, done) {
            process.nextTick(function(){
                User.findOne({'google.id':profile.id},function(err,user){
                    if(err){
                        return done(err);
                    }
                    if(user){
                        return done(null,user);
                    }else{
                        var newUser=new User();
                        newUser.google.id=profile.id;
                        newUser.google.token=accessToken;
                        newUser.google.name=profile.displayName;
                        newUser.google.email=profile.emails[0].value;

                        newUser.save(function(err){
                            if(err){
                                throw err;
                            }
                            return done(null,newUser);
                        })
                    }
                });
            });
        }
    ));




};

the callbackURL is

http://localhost:3000/auth/google/callback

what do i have to do in order to fix this issue ?

like image 360
menaka Avatar asked Jul 27 '16 13:07

menaka


1 Answers

Banged my head on this for awhile - seems like there's an old link reference in the basic strategy. Recommend you try

https://github.com/jaredhanson/passport-google-oauth2

instead. It's up-to-date and worked immediately for me!

*The debug solution got me to this solve, btw.

like image 68
Jared Alessandroni Avatar answered Oct 19 '22 10:10

Jared Alessandroni