我在node.js中構建了一個允許使用twitter帳戶登錄的應用程序。使用twitter oAuth和Passport.js登錄
我第一次運行代碼,機智工作,但幾天後它只是返回一些錯誤。
我試圖改變我的twitter應用程序配置,但它不起作用。
app.js:
var express = require('express')
, io
, _appS = require('./appServer')
, passport = require('passport')
, TwitterStrategy = require('passport-twitter').Strategy
;
var app = express()
, PORT = 7777
;
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
passport.use(new TwitterStrategy ({
consumerKey : "consumer",
consumerSecret : "secret",
callbackURL : "http://127.0.0.1:7777/auth/twitter/callback"
},
function (token, tokenSecret, profile, done) {
process.nextTick (function() {
return done (null, profile);
});
}
));
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/static'));
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({secret : 'argentina campeon'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.engine('jade', require('jade').__express);
});
io = require('socket.io').listen(app.listen(PORT));
/**
* ROUTES
*/
app.get(
"/",
function (req, res) {
if (req.user) {
//hay usuario
io.sockets.on('connection', function (socket) {
_appS.init(req.user.id, socket);
});
}
else
res.redirect('/login');
}
);
app.get(
"/login",
function (req, res) {
if (! req.user)
res.render("login");
else
res.redirect('/');
}
);
app.get(
"/auth/twitter",
passport.authenticate('twitter'),
function (req, res) { }
);
app.get(
"/auth/twitter/callback",
passport.authenticate('twitter', { failureRedirect : '/login' }),
function (req, res) {
console.log(req.user);
res.redirect('/');
}
);
app.get(
"/logout",
function (req, res) {
req.logout();
res.redirect('/');
}
);
當我嘗試登錄,它重定向我到Twitter要驗證的應用程序的權限,但它說:
We are redirecting you to the application. This may take a few minutes.
所以,如果我等待幾秒,一個新的鏈接出現:
If your browser does not redirect you, please click here
所以,當我點擊它,它現在將我重定向到我的應用程序,但現在我得到荷蘭國際集團的錯誤:
Error: Failed to find request token in session at Strategy.OAuthStrategy.authenticate (C:\Program Files\nodejs\node_modules\passport-twitter\node_modules\passport-oauth1\lib\strategy.js:142:54) at Strategy.authenticate (C:\Program Files\nodejs\node_modules\passport-twitter\lib\strategy.js:85:40) at attempt (C:\Program Files\nodejs\node_modules\passport\lib\middleware\authenticate.js:337:16) at authenticate (C:\Program Files\nodejs\node_modules\passport\lib\middleware\authenticate.js:338:7) at callbacks (C:\Program Files\nodejs\node_modules\express\lib\router\index.js:164:37) at param (C:\Program Files\nodejs\node_modules\express\lib\router\index.js:138:11) at pass (C:\Program Files\nodejs\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Program Files\nodejs\node_modules\express\lib\router\index.js:173:5) at Object.router (C:\Program Files\nodejs\node_modules\express\lib\router\index.js:33:10) at next (C:\Program Files\nodejs\node_modules\express\node_modules\connect\lib\proto.js:193:15)
有趣的是,在我的控制檯,我登錄後可以看到來自Twitter的所有用戶數據,因爲在我的代碼中的一些部分我把if (req.user) console.log(req.user);
- 我使用最新版本的Node.js和其包裝的米(快遞,護照,Twitter的護照,socket.io)
- 我使用Windows 7
所以,我能做些什麼來解決這個問題?
這行是錯誤的嗎? –