【问题标题】:Passport w/node.js Google errorPassport w/node.js Google 错误
【发布时间】:2013-02-19 06:52:16
【问题描述】:

我正在尝试将护照与 express/node.js 应用程序一起使用。由于某种原因,我无法使用谷歌进行身份验证。谷歌给出以下错误:

错误:invalid_request
解析 OpenID 身份验证请求时出错。

Code:

    passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(user, done) {
  done(null, user);
});

var GoogleStrategy = require('passport-google').Strategy;
var UserModel = require('./models/usermodel');

passport.use(new GoogleStrategy({
    returnURL: 'http://localhost:3000/auth/google/return',
    realm: 'http:/localhost:3000'
  },
  function(identifier, profile, done) {
    profile.email = profile.emails[0].value;
    UserModel.findOneAndUpdate({email:profile.email}, {$set:profile, $inc:{logins:1}}, {upsert:true}, done);
  }
));

【问题讨论】:

标签: node.js express passport.js


【解决方案1】:

当包含端口号时,returnURL 似乎不能很好地播放。 这意味着您可能需要切换到标准端口 (80),除非您调整操作系统以允许非 root 用户访问该端口,否则该端口在某些 linux 风格上将不起作用。不过,如上所述,在深入研究之前,我会考虑使用 OAuth 2.0。

【讨论】:

    【解决方案2】:

    第一件事。确保您的宝石是最新的。因此,将其添加到您的 package.json 中,然后运行 ​​npm install 或 npm update。我 99% 确定这是因为一个旧的 google gem 打破了我的。

    {
      "name": "Boilerplate",
      "version": "0.0.2",
      "private": true,
      "scripts": {
        "start": "node app"
      },
      "dependencies": {
        "express": "3.1.0",
        "ejs": "*",
        "stylus": "*",
        "mongoose": "*",
        "connect-mongo": "*",
        "passport": "*",
        "passport-google": "*",
        "passport-twitter": "*",
        "passport-facebook": "*",
        "passport-local": "*"
      }
    }
    

    然后运行:

    npm install
    

    这是我的 server.js 上的护照信息

    passport.serializeUser(function(user, done) {
      done(null, user.id)
    })
    
    passport.deserializeUser(function(id, done) {
      UserModel.findOne({ _id: id }, function (err, user) {
        done(err, user)
      })
    })
    
    var GoogleStrategy = require('passport-google').Strategy;
    var UserModel = require('./models/usermodel'); // A schema that is not optional 
    
    passport.use(new GoogleStrategy({
        returnURL: 'http://localhost:3000/auth/google/return',
        realm: 'http://localhost:3000'
      },
      function(identifier, profile, done) {
        console.log(profile);
          UserModel.findOneAndUpdate({email:profile.email}, {$set:profile, $inc:{logins:1}}, {upsert:true}, done);
      }
    ));
    

    【讨论】:

      【解决方案3】:

      我知道这有多晚了,但我认为值得注意的是您的领域不正确,这可能就是您收到此错误的原因。协议后面只有一个斜杠 -- http:/local 而不是 https://local

      【讨论】:

        【解决方案4】:

        如果您没有其他理由使用 OpenID,我强烈建议您切换到在 passport-google-oauth 中实现的 OAuth 2.0。

        Google 似乎建议将其作为他们首选的身份验证解决方案,而他们的 OpenID 实施似乎受到的关注较少,并且会出现虚假错误。

        【讨论】:

          猜你喜欢
          • 2015-03-09
          • 2013-03-20
          • 2014-08-05
          • 2015-04-22
          • 1970-01-01
          • 2021-01-14
          • 2012-08-15
          • 2018-05-25
          • 1970-01-01
          相关资源
          最近更新 更多