2015-01-08 77 views
2

目前,我爲我的iOS應用程序使用帶有Express和Passport-facebook-token模塊的NodeJS。我遇到了這個問題,因爲即使我們正在通過OAuth邏輯,服務總是返回401響應。這個代碼的任何想法,爲什麼它保持返回401,即使它已成功驗證,並來到我們的handleAuthentiation方法,但沒有達到我調用console.log("THIS IS NOT BEING CALLED")的方法?Passport-Facebook令牌不斷返回401

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

回答

1

想通了。原來我沒有正確初始化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()); 
+1

即時通訊使用sails.js框架獲得相同的問題,但是這些配置中的哪一個做了魔術? – simondelliott

+1

@simondelliott會議和護照的人做的伎倆 –

相關問題