Node.js Passport Twitter-错误:在会话中找不到请求令牌

Node.js Passport Twitter-错误:在会话中找不到请求令牌,node.js,twitter,passport.js,Node.js,Twitter,Passport.js,我正在使用passport twitter在我的网站上建立twitter连接。用户可以通过单击“登录”或“添加新项目”进行连接。2之间唯一的区别是,如果他们单击add new item,则登录后会打开一个模式窗口 要了解他们单击的按钮,我将url存储在req.session.referer: // route for twitter authentication and login app.get('/auth/twitter', function(req, res, next){ re

我正在使用passport twitter在我的网站上建立twitter连接。用户可以通过单击“登录”或“添加新项目”进行连接。2之间唯一的区别是,如果他们单击add new item,则登录后会打开一个模式窗口

要了解他们单击的按钮,我将url存储在
req.session.referer

// 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)
});
在我的开发环境中一切正常,但一旦联机,我会收到以下错误消息: 快车

我的设置在Twitter中设置正确。以下是我从日志中得到的信息: 关于请求:

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

{ cookie: 
    { path: '/',
      _expires: null,
      originalMaxAge: null,
      httpOnly: true },
   passport: {} }
可能是由于子域问题(vs),因为我在本地没有pb。 我怎样才能解决这个问题

非常感谢

编辑:我的API密钥设置如下(根据本教程:):


我也犯了同样的错误,但还是解决了。。下面是我的问题和解决方案


实际上,它不喜欢我的重定向url“http://localhost/auth/twitter/callback”。我把它改为“http://127.0.0.1/auth/twitter/callback”。在我的实际代码中,我必须将其保留为localhost,否则我将首先得到关于缺少令牌的错误。如果您的回调是localhost,并且您正在使用express session,请确保cookie secure选项设置为false。e、 g

// 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选项设置为Strict。把它改成Lax。e、 g

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

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

你们在哪里有tour twitter api密钥设置?我用这些信息编辑了我的问题。请看上面
if (process.env.NODE_ENV === 'production') {
    app.set('trust proxy', 1) // trust first proxy
    sess.cookie.secure = true // serve secure cookies
}
let sess = {
    secret: 'secret',
    resave: true,
    saveUninitialized: true,

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