Google OAuth2回调错误

时间:2017-10-17 02:53:03

标签: mongodb express oauth-2.0 google-oauth

非常非常新的用户身份验证。希望用户注册Google。

当前状态:我已成功完成此步骤 - (隐藏我的电子邮件) Google Account Selection

但是,当我点击一个帐户时,我收到此错误(500)(使用longjohn获取更长的堆栈跟踪):

Error
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:95:21
    at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
    at IncomingMessage.Readable.on (_stream_readable.js:689:35)
    at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at TLSSocket.socketOnData (_http_client.js:411:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at TLSSocket.Readable.push (_stream_readable.js:136:10)
    at TLSWrap.onread (net.js:560:20)
---------------------------------------------
    at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
    at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
    at exports.OAuth2.get (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:227:8)
    at Strategy.userProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:84:16)
    at loadIt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:345:17)
    at Strategy.OAuth2Strategy._loadUserProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:360:25)
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:168:16
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:209:7
    at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:134:9)
    at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
    at IncomingMessage.Readable.on (_stream_readable.js:689:35)
    at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at TLSSocket.socketOnData (_http_client.js:411:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at TLSSocket.Readable.push (_stream_readable.js:136:10)
    at TLSWrap.onread (net.js:560:20)
---------------------------------------------
    at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
    at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
    at exports.OAuth2.getOAuthAccessToken (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:190:8)
    at loaded (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:164:20)
    at NullStore.verify (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/state/null.js:9:3)
    at Strategy.OAuth2Strategy.authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:210:26)
    at attempt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:348:16)
    at authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:349:7)
    at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:174:3)
    at router (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:317:13)
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
---------------------------------------------
    at handleOperationCallback (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:454:24)
    at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:490:9
    at authenticateStragglers (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:429:16)
    at Connection.messageHandler (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:463:5)
    at Socket.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:319:22)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at Socket.Readable.push (_stream_readable.js:136:10)
    at TCP.onread (net.js:560:20)

我的代码围绕此功能:

// routes.js
router.get('/auth/google', authController.google);
router.get('/oauth2callback', authController.googleCallback
);

// passport.js
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CONSUMER_KEY,
      clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
      callbackURL: "http://localhost:21015/auth/google/callback"
    },
    function(accessToken, refreshToken, profile, done) {
      const user = new User();
      user.google.id = profile.id;
      user.google.token = accessToken;
      user.google.name = profile.displayName;
      user.google.email = profile.emails[0].value;
      user.save(function(err) {
        if (err) throw err;
        return done(null, user);
      });
    }
));

// authController.js
const passport = require('passport');
exports.google = passport.authenticate('google', {
  scope: 'https://www.google.com/m8/feeds'
});
exports.googleCallback = passport.authenticate('google', {
  failureRedirect: '/login', // if fail, where to go
  failureFlash: 'Failed Login!', // if fail, what to flash
  successRedirect: '/', // if success, where to go
  successFlash: 'You are now logged in!' // if success, what to flash
});

2 个答案:

答案 0 :(得分:1)

显然您无法使用IP地址作为终点,因此请尝试将您的回调网址更改为:http://localhost:21015/oauth2callback

答案 1 :(得分:0)

确保您已在https://console.cloud.google.com中启用了Google+ API。 要使用google Auth,您必须在Google控制台中启用API。一个非常常见的错误是,当您转到Google Developer Console为项目启用API时,首次要求项目要求您创建凭据。问题是在您创建凭据后,谷歌+ API(或任何其他API)无法自动启用,您应该从谷歌开发者控制台的项目页面启用它。 步骤进行:

  1. 转到Google Console
  2. 从顶部导航栏中选择您的项目(如果您还没有)
  3. 点击启用APIS和服务
  4. 在搜索框中搜索Google Plus
  5. 选择第一个结果Google+ Google+
  6. 点击启用
  7. 如果您没有看到“启用”按钮,请参阅“创建凭据”。按钮,表示您尚未创建凭据,并按照页面上的说明创建凭据。不要忘记在创建凭证后启用API。

    有关详细信息,请参阅Google Enable and disable APIs

    中的此说明

    除了上面提到的内容之外,您不需要为回调使用绝对网址,只需使用相对的网址即可:/auth/google/callback

    &#13;
    &#13;
    passport.use(new GoogleStrategy({
        clientID: process.env.GOOGLE_CONSUMER_KEY,
          clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
          callbackURL: "/auth/google/callback"
        },
        function(accessToken, refreshToken, profile, done) {
          const user = new User();
          user.google.id = profile.id;
          user.google.token = accessToken;
          user.google.name = profile.displayName;
          user.google.email = profile.emails[0].value;
          user.save(function(err) {
            if (err) throw err;
            return done(null, user);
          });
        }
    ));
    &#13;
    &#13;
    &#13;