Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js "passport-google-oauth2" delivers "failed to fetch user profile" error in Express application

While developing the last example of a node.js's introductory book (an express.js application using authentication strategy by Google OpenID), after replacing the passport-google package (which got obsolete on April 20th, 2015) with passport-google-oauth2 package (authentication strategy by Google OAuth 2.0) and having followed the indications at its documentation's page and an example here; I got the below error after selecting my Google+ account, which was thrown by the oath2.js module, concretely calling this._oauth2.get("https://www.googleapis.com/plus/v1/people/me",...) within userProfile(accessToken, done) method. The related source code and module dependencies are below.

What could be the root of the problem?

The concrete error is:

InternalOAuthError: failed to fetch user profile
    at <...>\web-app\b4\node_modules\passport-google-oauth2\lib\oauth2.js:92:28
    at passBackControl (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:9)
    at IncomingMessage.<anonymous> (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
    at IncomingMessage.emit (events.js:129:20)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

The related application's code is:

  passport = require('passport'),
  //...
  GoogleStrategy = require('passport-google-oauth2').Strategy; // #passport-google-oauth2
  //...
  /***** #passport-google-oauth2 vv *****/
  passport.use(new GoogleStrategy({
    clientID: "a_specific_value",
    clientSecret: "another_specific_value",
    callbackURL: "http://127.0.0.1:3000/auth/google/callback",
    passReqToCallback:true
  },
  function(request, accessToken, refreshToken, profile, done) {
      profile.identifier=profile.id;
      return done(null, profile);
  }
  ));
  /***** #passport-google-oauth2 ^^ *****/
  //...
  /*****  #passport-google-oauth2 vv    *****/
  app.get('/auth/google',
  passport.authenticate('google', { successRedirect: '/',scope:
    [ 'https://www.googleapis.com/auth/userinfo.email']})
  );
  app.get( '/auth/google/callback',
    passport.authenticate( 'google', {
        successRedirect: '/',
        failureRedirect: '/'
  }));
  /*****  #passport-google-oauth2 ^^    *****/    

The application has the following dependencies:

[email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│   └── [email protected]
├─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   └── [email protected]
├── [email protected]
├── [email protected]
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ └─┬ [email protected]
  │   └── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ └── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  ├── [email protected]
  └── [email protected]

like image 774
alesscor Avatar asked May 09 '15 22:05

alesscor


3 Answers

I just fortunately found a similar issue at jaredhanson/passport-google-oauth, which gave me the idea to go to the Google's project console and simply enable the Google+ API, which was "turned off" (oh me!!, naive developer of his first application based on Google+). That was the root of the problem. I tried again and the oauth2 started receiving profiles correctly.

like image 82
alesscor Avatar answered Oct 08 '22 03:10

alesscor


The scope you are using is deprecated now :

passport.authenticate('google', { successRedirect: '/',scope:   [ 'https://www.googleapis.com/auth/userinfo.email']}) ); 

Instead, we must use this one:

passport.authenticate('google', { successRedirect: '/',scope:   ['email'] })); 

You can also get the profile scope:

passport.authenticate('google', { successRedirect: '/',scope:   [ 'email', 'profile' ] })); 
like image 24
Rémi Becheras Avatar answered Oct 08 '22 05:10

Rémi Becheras


I'm using Google OAuth 2.0 Playground and in my case the reason for this error was that my token has simply expired. Refreshing it in Playground resolved the issue.

like image 45
1valdis Avatar answered Oct 08 '22 05:10

1valdis