什么是在我的帆.js护照.js实现的InternalOAuthError的原因

What is the cause of the InternalOAuthError in my sails.js passport.js implementation?

本文关键字:js InternalOAuthError 实现 护照 我的 什么      更新时间:2023-09-26

我试图在Sails.js应用程序中实现passport.js身份验证,使用Google OAuth2.0策略。我考虑过使用sails-generate-authsails-auth,但它们不再受支持。我也考虑过Waterlock,但它只适用于本地,Twitter和Facebook策略。

当用户按下"login with google+"按钮时,下面的google()函数被调用。预期的行为是,用户然后被重定向到一个Google页面,在那里他们被提示进行身份验证。实际上,以下错误记录在标记的行上。此时user对象是未定义的。

InternalOAuthError: Failed to obtain request token (status: 307 data: <HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved <A HREF="https://accounts.google.com/OAuthGetRequestToken">here</A>.
</BODY>
</HTML>
)

下面的函数位于AuthController中,并在点击"login with Google+"按钮时被调用。

google(request, response) {
    const google = sails.config.oauth.google;
    passport.use(new GoogleStrategy({
            consumerKey: MY_KEY,
            consumerSecret: MY_SECRET,
            callbackURL: 'http://localhost:1337/auth/callback/google',
        }, (token, tokenSecret, profile, done) => {
            //Verify callback: Never appears to get called.
            User.findOrCreate({ providerId: profile.id, provider: 'Google' }).then((user) => {
                request.session.me = user;
                return done(user);
            }).catch(error => {
                sails.log('Failed to log in using Google: ' + error);
            });
        }
    ));
    passport.authenticate('google', {
        failureRedirect: '/login',
        scope: [
            'profile',
        ],
    }, function(error, user) {
        //Gets called, but user is undefined.
        if (error) {
            sails.log('Err: ' + error); //<== Error gets logged here.
        }
        request.logIn(user, function(error) {
            if (error) {
                sails.log('err: ' + error);
                response.view('500');
                return;
            }
            res.redirect('/');
        });
    })(request, response);
},

其他信息:Passport is initialized in config/policies.js:

    '*': [
    passport.initialize(),
    passport.session(),
],

我的问题:我得到的错误可能的原因是什么?

我在使用Express、passport和passport-google-oath时遇到了同样的问题。我通过切换到passport-google-oauth20解决了这个问题。

我不能100%确定这是否是原因,但谷歌似乎已经放弃了OAuth支持相当长一段时间了。

重要:OAuth 1.0已于2012年4月20日正式弃用,不再支持。我们建议您尽快迁移到OAuth 2.0。

你必须将你的策略更改为:

var GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://www.example.com/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));