【问题标题】:Google Oauth giving code redeemed errorGoogle Oauth 提供代码兑换错误
【发布时间】:2015-09-18 23:10:21
【问题描述】:

嗨,我正在做一个用户通过谷歌帐户登录的项目。(本地主机) 我已经实现了谷歌注册。 一旦我从我的帐户登录,我就会收到以下错误。

TokenError: Code was already redeemed.
       at Strategy.OAuth2Strategy.parseErrorResponse (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:298:12)
       at Strategy.OAuth2Strategy._createOAuthError (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:345:16)
       at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:171:43
       at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:176:18
       at passBackControl (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9)
       at IncomingMessage.<anonymous> (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:142:7)
       at IncomingMessage.emit (events.js:129:20)
       at _stream_readable.js:908:16
       at process._tickCallback (node.js:355:11)

我的代码如下(sn-p for google login):-

passport.use(new GoogleStrategy(google, function(req, accessToken, refreshToken, profile, done) {
  if (req.user) {
    User.findOne({ google: profile.id }, function(err, existingUser) {
      if (existingUser) {
        console.log('There is already a Google+ account that belongs to you. Sign in with that account or delete it, then link it with your current account.' );
        done(err);
      } else {
        User.findById(req.user.id, function(err, user) {
          user.google = profile.id;
          user.tokens.push({ kind: 'google', accessToken: accessToken });
          user.profile.displayName = user.profile.displayName || profile.displayName;
          user.profile.gender = user.profile.gender || profile._json.gender;
            //user.profile.picture = user.profile.picture || 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
          user.save(function(err) {
            console.log('Google account has been linked.');
            done(err, user);
          });
        });
      }
    });
  } else {
    User.findOne({ google: profile.id }, function(err, existingUser) {
      if (existingUser) return done(null, existingUser);
      User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
        if (existingEmailUser) {
           console.log('There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.' );
          done(err);
        } else {
          var user = new User();
          user.email = profile._json.email;
          user.google = profile.id;
          user.tokens.push({ kind: 'google', accessToken: accessToken });
          user.profile.displayName = profile.displayName;
          user.profile.gender = profile._json.gender;
            //user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
          user.profile.location = (profile._json.location) ? profile._json.location.name : '';
          user.save(function(err) {
            done(err, user);
          });
        }
      });
    });
  }
}));

我卡住了。请帮帮我..谢谢

【问题讨论】:

    标签: node.js authentication express oauth passport.js


    【解决方案1】:

    问题不在于您的“sn-p”,请查看路线。它应该是 google 重定向的绝对路径。

    router.get('/auth/google/callback',
    passport.authenticate('google', { failureRedirect: '#/signIn' }),
    function(req, res) {
    // absolute path
        res.redirect('http://localhost:8888/#/home');
    });
    

    这是已知问题,请点击此链接查看其他解决方法 https://github.com/jaredhanson/passport-google-oauth/issues/82

    【讨论】:

    • 请注意,我遇到了一个问题,即我的应用程序卡在了身份验证步骤。如果要移动代码,请确保在新主机上重新安装 mongodb。
    【解决方案2】:

    我遇到了同样的问题。

    google console 重置客户端密码解决了这个问题。

    【讨论】:

      【解决方案3】:

      我遇到过这个问题。确切的问题是你的路线。

      app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
         res.send('get the data');
      });
      

      此时应用程序已获得用户许可,谷歌将代码发送到此 url。现在护照在这里做了什么,它使用该代码并向谷歌请求用户详细信息并从谷歌获得。现在我们必须对这些细节做一些事情,否则你会得到你得到的错误。

      现在我们可以使用护照的 serialiseUser 和 deserialiseUser 将详细信息保存在 cookie 中,并编辑上面的一行代码以访问类似的 url。

      app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
         res.redirect('/servey');  // just a url to go somewhere
      });
      

      【讨论】:

        【解决方案4】:

        几天前我也遇到了同样的问题。我想出的是,你只需要完成这个过程。到目前为止,您只检查了用户是否存在于数据库中。如果没有,则将用户保存到数据库中。

        但是,在此之后,当 google 尝试重定向用户时,google+ API 发送的代码已被使用,或者说它不再可用。因此,当您检查数据库中的用户时,您需要对用户进行序列化,即将代码存储到浏览器中的 cookie 中,以便当 google 重定向用户时,它知道用户是谁。这可以通过添加下面给出的代码来完成。

        //add this in current snippet
        passport.serializeUser(function(user,done){
            done(null,user.id);
        });
        

        要使用此 cookie,您需要反序列化用户。要反序列化,请使用下面给出的代码。

        //add this in current snippet
        passport.deserializeUser(function(id,done){
            User.findById(id).then(function(user){
                done(null, user);
            });
        });
        

        此外,您需要启动一个 cookie 会话,您可以通过在主 app.js 文件中添加以下代码来执行此操作。

        const cookieSession = require('cookie-session');
        app.use(cookieSession({
            maxAge: 24*60*60*1000, // age of cookie, the value is always given in milliseconds
            keys:[keys.session.cookiekey]
        }));
        
        //initialize passport
        app.use(passport.initialize());
        app.use(passport.session());
        

        请注意,您需要 cookie-session 包。使用安装它

        npm install cookie-session
        

        另外,您需要在您的 google 策略的 callbackURL 属性中写入绝对 URI。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-25
          • 2016-05-23
          • 1970-01-01
          • 2019-03-12
          • 2016-06-18
          • 1970-01-01
          相关资源
          最近更新 更多