Passport-facebook-token一直返回401

时间:2015-01-08 04:35:07

标签: ios node.js facebook passport.js passport-facebook

目前,我正在为我的iOS应用程序使用带有Express和Passport-facebook-token模块的NodeJS。我遇到了麻烦,因为即使我们通过OAuth逻辑,服务总是返回401响应。有关此代码的任何想法,即使它成功通过身份验证并继续使用我的handleAuthentiation方法但未达到console.log("THIS IS NOT BEING CALLED")调用的方法,仍会返回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

2 个答案:

答案 0 :(得分: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 :(得分:0)

对我来说,我使用了文档中的示例:https://www.npmjs.com/package/passport-facebook-token,所以我没有添加'.Strategy'

,对于问题中的方法“ POST”,我从前端作为正文发送:

const tokenBlob = new Blob([JSON.stringify({access_token: response.accessToken}, null, 2)], {type : 'application/json'}); 

https://medium.com/@alexanderleon/implement-social-authentication-with-react-restful-api-9b44f4714fa

中所述

祝你好运!