Passport-facebook-token 不断返回 401

Passport-facebook-token keeps returning 401

目前,我正在为我的 iOS 应用程序使用带有 Express 和 Passport-facebook-token 模块的 NodeJS。我在这方面遇到了麻烦,因为即使我们通过了 OAuth 逻辑,该服务也总是返回 401 响应。关于此代码为什么它一直返回 401 的任何想法,即使它成功通过身份验证并进入我们的 handleAuthentiation 方法但没有达到我在 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 方法被映射到 verified 然后调用 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());

对我来说,我使用了文档中的示例: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

祝你好运!