Spe*_*her 5 twitter node.js passport.js
我使用passport-twitter在我的网站上建立了一个twitter连接.用户可以通过单击"登录"或"添加新项目"进行连接.2之间的唯一区别是,如果他们点击添加新项目,则一旦他们登录就会打开模态窗口.
要知道他们点击的按钮,我将网址存储在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)
});
Run Code Online (Sandbox Code Playgroud)
在我的开发环境中一切正常,但一旦在线,我收到此错误消息: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)
...
Run Code Online (Sandbox Code Playgroud)
我的设置在Twitter中正确设置.以下是我对日志的了解:对于请求:
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
passport: {},
referrer: '/auth/twitter' }
Run Code Online (Sandbox Code Playgroud)
对于回调:
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
passport: {} }
Run Code Online (Sandbox Code Playgroud)
也许这可能是由于子域问题(http://example.com vs http://www.example.com),因为我没有本地的pb.我怎样才能解决这个问题?
非常感谢
编辑:我的API密钥设置如下(根据本教程: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) {
...
});
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的错误,但解决了它..这就是我的问题和解决方案。
事实上,它不喜欢我的重定向网址“http://localhost/auth/twitter/callback”。我将其更改为“http://127.0.0.1/auth/twitter/callback”。在我的实际代码中,我必须将其保留为本地主机,否则我会收到有关丢失令牌的错误