Node.js "passport-google-oauth2" 在 Express 应用程序中传递 "failed to fetch user profile" 错误

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

正在开发最后一个例子's introductory book (an 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;选择我的 Google+ 帐户后出现以下错误,这是由 oath2.js 模块抛出的,具体是在 userProfile(accessToken, done) 方法中调用 this._oauth2.get("https://www.googleapis.com/plus/v1/people/me",...)。相关源码和模块依赖如下。

问题的根源可能是什么?

具体错误为:

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)

相关应用程序代码为:

  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 ^^    *****/    

该应用程序具有以下依赖项:

b4@0.0.1
├─┬ connect-redis@1.4.7
│ ├─┬ debug@2.1.3
│ │ └── ms@0.7.0
│ └── redis@0.10.3
├─┬ cookie-parser@1.3.4
│ ├── cookie@0.1.2
│ └── cookie-signature@1.0.6
├─┬ express@3.3.8
│ ├── buffer-crc32@0.2.1
│ ├─┬ commander@1.2.0
│ │ └── keypress@0.1.0
│ ├─┬ connect@2.8.8
│ │ ├── bytes@0.2.0
│ │ ├── formidable@1.0.14
│ │ ├── pause@0.0.1
│ │ ├── qs@0.6.5
│ │ └── uid2@0.0.2
│ ├── cookie@0.1.0
│ ├── cookie-signature@1.0.1
│ ├─┬ debug@2.1.3
│ │ └── ms@0.7.0
│ ├── fresh@0.2.0
│ ├── methods@0.0.1
│ ├── mkdirp@0.3.5
│ ├── range-parser@0.0.4
│ └─┬ send@0.1.4
│   └── mime@1.2.11
├─┬ express-session@1.11.1
│ ├── cookie@0.1.2
│ ├── cookie-signature@1.0.6
│ ├── crc@3.2.1
│ ├─┬ debug@2.1.3
│ │ └── ms@0.7.0
│ ├── depd@1.0.1
│ ├── on-headers@1.0.0
│ ├── parseurl@1.3.0
│ ├─┬ uid-safe@1.1.0
│ │ ├── base64-url@1.2.1
│ │ └── native-or-bluebird@1.1.2
│ └── utils-merge@1.0.0
├─┬ morgan@1.5.2
│ ├── basic-auth@1.0.0
│ ├─┬ debug@2.1.3
│ │ └── ms@0.7.0
│ ├── depd@1.0.1
│ └─┬ on-finished@2.2.0
│   └── ee-first@1.1.0
├─┬ npmlog@0.0.4
│ └── ansi@0.1.2
├─┬ passport@0.2.1
│ ├── passport-strategy@1.0.0
│ └── pause@0.0.1
├─┬ passport-google-oauth2@0.1.6
│ └─┬ passport-oauth2@1.1.2
│   ├── oauth@0.9.12
│   ├── passport-strategy@1.0.0
│   └── uid2@0.0.3
├── q@0.9.7
├── redis@0.8.6
└─┬ request@2.27.0
  ├── aws-sign@0.3.0
  ├── cookie-jar@0.3.0
  ├── forever-agent@0.5.2
  ├─┬ form-data@0.1.4
  │ ├── async@0.9.0
  │ └─┬ combined-stream@0.0.7
  │   └── delayed-stream@0.0.5
  ├─┬ hawk@1.0.0
  │ ├── boom@0.4.2
  │ ├── cryptiles@0.2.2
  │ ├── hoek@0.9.1
  │ └── sntp@0.2.4
  ├─┬ http-signature@0.10.1
  │ ├── asn1@0.1.11
  │ ├── assert-plus@0.1.5
  │ └── ctype@0.5.3
  ├── json-stringify-safe@5.0.0
  ├── mime@1.2.11
  ├── node-uuid@1.4.3
  ├── oauth-sign@0.3.0
  ├── qs@0.6.6
  └── tunnel-agent@0.3.0

幸运的是我在 jaredhanson/passport-google-oauth, which gave me the idea to go to the Google's project console 并简单地启用 Google+ API,它是 "turned off"(天哪!!他的第一个基于 Google+ 的应用程序的天真开发者)。这就是问题的根源。我再次尝试,oauth2 开始正确接收配置文件。

您正在使用的 scope 现已弃用:

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

相反,我们必须使用这个:

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

您还可以获得 profile scope:

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

我正在使用 Google OAuth 2.0 Playground,在我的情况下,出现此错误的原因是我的令牌已过期。在 Playground 中刷新它解决了这个问题。

我发现了同样的错误,我这样做解决了它:

在 package.json 中,将 "passport" 值更改为“^0.4.0”,将 "passport-google-oauth" 更改为“^2.0.0”。 运行 "npm install" 再一次。

我也遇到了同样的问题! 解决方案:
内部 passport.use(新的谷歌策略({ clientID: "a_specific_value", clientSecret: "another_specific_value", callbackURL: "http://127.0.0.1:3000/auth/google/callback", passReqToCallback:true, ********************************* },

在凝视的行上添加这个 link "https://www.googleapis.com/oauth2/v3/userinfo" 最终代码将如下所示 passport.use(新的谷歌策略({ clientID: GOOGLE_CLIENT_ID, clientSecret:.GOOGLE_CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/secrets", passReqToCallback:true, userProfileURL:"https://www.googleapis.com/oauth2/v3/userinfo" }, 希望能解决问题![​​=10=]

经过一些研究,我的问题的根本原因是我最初使用

userProfileURL:"https://googleapis.com/oauth2/v3/userinfo" (WRONG)

而不是使用

userProfileURL:"https://**www**.googleapis.com/oauth2/v3/userinfo"

Screenshot

当我将我的包从 passport-google-oauth 更改为 passport-google-oauth20 时,一切都开始正常工作。

使用不在 Test Users 列表(在 Google's project console 中的 OAuth consent screen 下)的用户登录未发布的 OAuth 应用程序时也会抛出此错误。