node.js - Passport-Twitter - 错误 : Failed to find request token in session

标签 node.js twitter passport.js

我正在使用 passport-twitter 在我的站点上设置一个 twitter 连接。用户可以通过单击“登录”或“添加新项目”进行连接。两者之间的唯一区别是,如果他们单击添加新项目,则模式窗口应该会在他们登录后打开。

要了解他们点击的按钮,我将 url 存储在 req.session.referrer 中:

// route for twitter authentication and login
app.get('/auth/twitter', function(req, res, next){
    req.session.referrer = req.url;
    console.log(req.session);
    passport.authenticate('twitter')(req, res, next);
});

app.get('/auth/twitter/new', function(req, res, next){
    req.session.referrer = req.url;
    console.log(req.session);
    passport.authenticate('twitter')(req, res, next);
});

// handle the callback after twitter has authenticated the user
app.get('/auth/twitter/callback', function(req, res, next){
    var options = {
            successRedirect : '/twitter-user/signin',
            failureRedirect : '/'
        };

    console.log(req.session);
    if (req.session.referrer && req.session.referrer.indexOf('new') > -1) options.successRedirect = '/twitter-user/new';

    passport.authenticate('twitter', options)(req, res, next)
});

在我的开发环境中一切正常,但在线后我收到此错误消息: express

500 Error: Failed to find request token in session
at Strategy.OAuthStrategy.authenticate (/app/node_modules/passport-twitter/node_modules/passport-oauth1/lib/strategy.js:142:54)
...

我的设置已在 Twitter 中正确设置。这是我从日志中得到的: 对于请求:

{ cookie: 
  { path: '/',
      _expires: null,
      originalMaxAge: null,
      httpOnly: true },
   passport: {},
   referrer: '/auth/twitter' }

对于回调:

{ cookie: 
    { path: '/',
      _expires: null,
      originalMaxAge: null,
      httpOnly: true },
   passport: {} }

可能是由于子域问题(http://example.com vs http://www.example.com),因为我在本地没有 pb。 我该如何解决这个问题?

非常感谢

编辑:我的 API key 是这样设置的(根据本教程:http://scotch.io/tutorials/javascript/easy-node-authentication-twitter):

passport.use(new TwitterStrategy({

    consumerKey     : configAuth.twitterAuth.consumerKey,
    consumerSecret  : configAuth.twitterAuth.consumerSecret,
    callbackURL     : configAuth.twitterAuth.callbackURL

},function(token, tokenSecret, profile, done) {
    ...
    });

最佳答案

首先,如果您的回调是本地主机并且您正在使用快速 session ,请确保将 cookie 安全选项设置为 false。例如

// Express session
let sess = {
    secret: 'secret',
    resave: true,
    saveUninitialized: true,

    cookie: {
        secure: false,
    }
}

您也可以通过以下方式在生产中更改它

if (process.env.NODE_ENV === 'production') {
    app.set('trust proxy', 1) // trust first proxy
    sess.cookie.secure = true // serve secure cookies
}

如果这不起作用,请检查您是否设置了 cookie sameSite选择严格。将其更改为松散。例如

let sess = {
    secret: 'secret',
    resave: true,
    saveUninitialized: true,

    cookie: {
        secure: false,
        sameSite: 'Lax'
    }
}

关于node.js - Passport-Twitter - 错误 : Failed to find request token in session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295662/

相关文章:

javascript - Node JS 中以字符串形式获取另一个程序的连续输出

javascript - 如何在客户端浏览器中使用 Node 缓冲区模块 - 请详细说明

iphone - OAuthTwitterController 委托(delegate)崩溃

node.js - Passport + Express V4 要求用户未定义

javascript - 注册 Passport.js 和 Sails.js 后如何重定向到特定位置?

node.js - 将 Apollo Express 服务器部署到 Heroku 时出现问题 : "GET query missing."

node.js - FCM : getting ' Error: data must be a non-null object' error?

twitter - tweepy 未授权 - tweepy.error.TweepError : Not authorized

iphone - iOS 推特账户访问

authentication - 使用koa和passport进行认证