Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – Google Oauth提供代码兑换错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我正在开发一个用户通过谷歌帐户登录的项目.(localhost)
我已经实施了谷歌注册.
一旦我从我的帐户登录,我收到以下错误.

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)

我的代码如下(谷歌登录代码段): –

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,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 },exisTingUser) {
      if (exisTingUser) return done(null,exisTingUser);
      User.findOne({ email: profile._json.email },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);
          });
        }
      });
    });
  }
}));

我被困在上面.请帮帮我……谢谢

解决方法

问题不在你的“片段”中,请看路线.它应该是谷歌重定向绝对路径.

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

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

大佬总结

以上是大佬教程为你收集整理的node.js – Google Oauth提供代码兑换错误全部内容,希望文章能够帮你解决node.js – Google Oauth提供代码兑换错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。