目前,我正在为iOS应用程序使用带有Express和Passport-facebook-token模块的NodeJS。我遇到了麻烦,因为即使我们通过了OAuth逻辑,服务也总是返回401响应。关于此代码的任何想法,即使它成功通过身份验证并进入我们的handleAuthentiation
方法,但为何仍返回401,但仍达不到我叫出console.log("THIS IS NOT BEING CALLED")
的方法,为什么?
var http = require("http");
var express = require("express");
var bodyParser = require('body-parser');
var database = require('./database.js');
var passport = require('passport'),
FacebookTokenStrategy = require('passport-facebook-token').Strategy;
var User = require('./User.js');
var app = express();
//setup json parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//setup facebook authentication
passport.use(new FacebookTokenStrategy({
clientID: "2454efafd33r",
clientSecret: "323245343"
},
function(accessToken, refreshToken, profile, done) {
User.handleAuthentication(accessToken, refreshToken, profile, done, function(err, user) {
console.log("handleAuthentication user is " + JSON.stringify(user));
console.log("handleAuthentication error is " + JSON.stringify(err));
done(err, user);
});
}
));
app.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function(req, res) {
console.log("THIS IS NOT BEING CALLED");
// do something with req.user
res.status(200);
res.send(user);
}
);
http.createServer(app).listen(3000);
我试图通过其他库跟踪调用,但似乎找不到定义了
success
的任何方法。我注意到done
方法已映射到经过验证的方法,然后调用了success
。 最佳答案
弄清楚了。原来我没有正确初始化Oauth。我需要设置以下内容:
// Configure Express
app.use(logger('combined'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(session({secret: 'supernova', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());