passport-google-oauth2 icon indicating copy to clipboard operation
passport-google-oauth2 copied to clipboard

passBackControl errors

Open ghost opened this issue 8 years ago • 11 comments

Error
    at /Users/theonlygusti/Project/node_modules/passport-google-oauth20/lib/strategy.js:95:21
    at passBackControl (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

I get the above error after: going to /sign-in/go, clicking my google account from the list google gives me, then ... error (url looks like http://localhost:61337/sign-in/after?code=4/BIsK3AIVt0p-Lk5ZGsyVqrv3KG3FevvO_ZPeKTwF6Tm# at this stage.)

Here's my code:

const express=require('express');
const app = express();
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_OAUTH_CLIENT_ID,
  clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET,
  callbackURL: '/sign-in/after'
},
function(accessToken, refreshToken, profile, cb) {
  User.findOrCreate({ googleId: profile.id }, function (err, user) {
    return cb(err, user);
  });
}));

app.get('/sign-in/go', passport.authenticate('google', { scope: ['profile'] }));

app.get('/sign-in/after',
  passport.authenticate('google', { failureRedirect: '/sign-in' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

const httpServer = app.listen(app.get('port'));

ghost avatar Nov 09 '17 17:11 ghost

@theonlygusti Try replace the callbalckURL with full address http://.... or https:// for example: callbackURL: 'http://localhost:5000/google/callback' Also make sure this URL is registered in your app Console Hope it helps

natanbr avatar Nov 14 '17 02:11 natanbr

I don't know the host or scheme, how do I figure those out?

nodingneu avatar Nov 14 '17 07:11 nodingneu

@theonlygusti I was just having this error, make sure you enabled the Google+ API in the developer console.

reece-bennett avatar Nov 18 '17 23:11 reece-bennett

None of this worked for me.

johnRivs avatar Nov 20 '17 21:11 johnRivs

I can share with you my code, hope it will help @johnRivs , @theonlygusti

  passport.use('google', new GoogleStrategy({
    clientID: process.env.ClientID,
    clientSecret: process.env.ClientSecret,
    callbackURL: process.env.CallbackURL, // => http://localhost:5000/auth/google/callback
    scope: [
      'https://www.googleapis.com/auth/userinfo.profile',
      'profile',
      'https://www.googleapis.com/auth/plus.login',
      'https://www.googleapis.com/auth/userinfo.email'],
    passReqToCallback: true
  },
    ((req, accessToken, refreshToken, profile, done)=> {
      return handle()
        .then((user)=> done(null, user))
        .catch((err)=> done(err, null));
    })
  ));
router.get('/auth/:provider',
  (req, res, next)=> {
    passport.authenticate(`google`, {
      authType: 'rerequest',
      accessType: 'offline',
      prompt: 'consent',
      includeGrantedScopes: true
    })(req, res, next);
  }
);
router.get('/auth/google/callback', (req, res, next)=> {
    const successRedirect = `/google?response=success`;
    const failureRedirect = `/google?response=fail`;
    passport.authenticate(`${req.params.provider}`, { successRedirect, failureRedirect })(req, res, next);
});

Update:

const GoogleStrategy = require('passport-google-oauth20').Strategy;

natanbr avatar Nov 21 '17 07:11 natanbr

I saw this error in production logs recently. This line suggests it's an error from the Google API

My only concern is that I don't seem to be calling Strategy.prototype.userProfile from anywhere in my code. So, I can't figure out how to catch these errors and prevent them from crashing the process. I believe this is called by the Strategy constructor or by passport itself. Does anyone know how I can catch these errors?

cmmartin avatar Dec 12 '17 23:12 cmmartin

@theonlygusti I have this problem too and none of the above helped me solve it. As @cmmartin suggests, I guess it's a Google API error because debugging the passBackControl method at line lib/oauth2.js:132:9 the response has a 403 Forbidden status, but I can't find any more details in the response.

I triple-checked the Google Developers console and the routes there match exactly the routes in the passsport configuration. It gets to /auth/google/callback as OP suggested, but with an error coming from the Google API. I'll try to dig deeper but already lost 3 hours on this.


EDIT - Managed to get it working So @PhyscoKillerMonkey was right I guess. The Google + Api had to be enabled on Google Developers. I was sure I enabled it yesterday but I am logged in with 2 different google accounts and each has different projects, and I guess I accidentally enabled it for another project. Today I checked the project I'm working on and didn't have it enabled. After enabling it - boom - it works.

It kinda sucks that there's no proper error message or statement that says that the Google+ Api has to be enabled.

However, in the meanwhile I also moved authentication using the Google Node API because I also need other calls like getting the user's calendar and so on. While doing their Usage tutorial I read that I have to enable the Google+ API and observed that I didn't do it yesterday. Anyhow, hope this helps @theonlygusti

raulrene avatar Jan 21 '18 19:01 raulrene

I had the same problem and as @PhyscoKillerMonkey mentioned, enabling Google + Api worked for me

vemurikarthik avatar Mar 13 '18 07:03 vemurikarthik

I had the same problem and as someone above mentioned, enabling Google + Api worked!

p.s: only after enabling Google +Api that google starts to track your API's percentage usage.

pedrogglima avatar Apr 24 '19 20:04 pedrogglima

Hi, Can you tell me where I need to add the good+ api key in my project to get it to work please? I am build a react app, google works perfectly on localhost but when I upload the website on my server it brings back an error 403.

highviewstudios avatar Jul 03 '20 12:07 highviewstudios

Hi @highviewstudios, best to store it as an environment variable on your deployment server, and access it via process.env.GOOGLE__API_KEY in your application code or something like that. Storing it in the code is never advisable.

toritsejuFO avatar Jul 06 '20 19:07 toritsejuFO