Mongodb Google OAuth2回调错误

Mongodb Google OAuth2回调错误,mongodb,express,oauth-2.0,google-oauth,Mongodb,Express,Oauth 2.0,Google Oauth,对用户身份验证来说非常非常新。希望允许用户在谷歌注册 当前状态:我正在成功地完成这一步- (隐藏我的电子邮件) 但是,当我单击一个帐户时,我得到了这个错误(500)(使用longjohn进行更长的堆栈跟踪): 显然,您不能将IP地址用作和端点,因此请尝试将回调url更改为:http://localhost:21015/oauth2callback确保在中启用了Google+API。 要使用google Auth,您必须在google控制台中启用API。一个非常常见的错误是,当您转到Google

对用户身份验证来说非常非常新。希望允许用户在谷歌注册

当前状态:我正在成功地完成这一步- (隐藏我的电子邮件)

但是,当我单击一个帐户时,我得到了这个错误(500)(使用longjohn进行更长的堆栈跟踪):


显然,您不能将IP地址用作和端点,因此请尝试将回调url更改为:
http://localhost:21015/oauth2callback

确保在中启用了Google+API。 要使用google Auth,您必须在google控制台中启用API。一个非常常见的错误是,当您转到Google开发者控制台为您的项目启用API时,它第一次要求您创建凭据。问题是,在您创建凭据之后,google+API(或任何其他API)将不会自动启用,您应该从google开发者控制台的项目页面启用它。 步骤:

  • 从顶部导航选择您的项目(如果尚未选择)
  • 单击启用API和服务
  • 在搜索框中搜索Google Plus
  • 选择第一个结果,Google+API
  • 单击启用
  • 如果您没有看到“启用”按钮和“创建凭据”按钮,则表示您尚未创建凭据,并按照页面上的说明创建凭据。不要忘记在创建凭据后启用API

    有关更多信息,您可以从谷歌阅读本说明

    除上述内容外,回调不需要使用绝对URL,只需使用相对URL,如:
    /auth/google/callback

    passport.use(谷歌新策略)({
    clientID:process.env.GOOGLE\u CONSUMER\u KEY,
    clientSecret:process.env.GOOGLE\u CONSUMER\u SECRET,
    callbackURL:“/auth/google/callback”
    },
    函数(accessToken、refreshToken、profile、done){
    const user=新用户();
    user.google.id=profile.id;
    user.google.token=accessToken;
    user.google.name=profile.displayName;
    user.google.email=profile.emails[0]。值;
    user.save(函数(err){
    如果(错误)抛出错误;
    返回完成(空,用户);
    });
    }
    
    ));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
    });