我正在寻找一种方法,让我的客户端使用facebook JS SDK进行授权,然后以某种方式将此授权传输到我的节点服务器(这样它就可以使用fb图形api验证请求)。
我偶然发现了:https://github.com/jaredhanson/passport-facebook/issues/26
&
https://github.com/drudge/passport-facebook-token
这似乎是一种与passport-facebook完全不同的策略。
我假设的正确吗:
用户使用fb JS SDK登录,然后facebook-token策略以某种方式从文档或正文对象中提取令牌和fb id?
或者,有没有其他像样的方法来实现这一点?也就是说,我试图避免由服务器SDK强制执行的重定向
发布于 2014-06-29 17:45:29
这周我花了几天的时间,试图找出使用Facebook身份验证私有API的最佳方式,使用passport.js - passport-facebook-token就是最好的方式。
您假设这是两个独立的身份验证策略,这是正确的。你不需要安装passport-facebook来使用passport-facebook-token。
如果您在客户端JS (或iOS等)中实现了Facebook身份验证,并且正在寻找一种使用用户的Facebook令牌对API请求进行身份验证的方法,那么authToken -facebook-token是一个非常优雅的解决方案。
passport- Facebook -token完全独立于passport-facebook工作,基本上是在内部处理Facebook所需的重定向,然后将请求传递给您的控制器。
因此,要使用passport-facebook-token验证API路由,您需要设置一个passport策略,如下所示:
passport.use('facebook-token', new FacebookTokenStrategy({
clientID : "123-your-app-id",
clientSecret : "ssshhhhhhhhh"
},
function(accessToken, refreshToken, profile, done) {
// console.log(profile);
var user = {
'email': profile.emails[0].value,
'name' : profile.name.givenName + ' ' + profile.name.familyName,
'id' : profile.id,
'token': accessToken
}
// You can perform any necessary actions with your user at this point,
// e.g. internal verification against a users table,
// creating new user entries, etc.
return done(null, user); // the user object we just made gets passed to the route's controller as `req.user`
}
));
值得注意的是,passport-facebook-token自述文件中使用的User.findOrCreate
方法不是默认的mongo/mongoose方法,而是一个插件,如果需要的话,您必须安装它。
要将此身份验证策略用作任何路由的中间件,您需要将一个access_token
对象作为URL参数或请求主体的属性传递给它。
app.get('/my/api/:access_token/endpoint',
passport.authenticate(['facebook-token','other-strategies']),
function (req, res) {
if (req.user){
//you're authenticated! return sensitive secret information here.
res.send(200, {'secrets':['array','of','top','secret','information']});
} else {
// not authenticated. go away.
res.send(401)
}
}
注意:access_token
属性区分大小写并使用下划线。passport-facebook-token的文档并不多,但是它的源码有很好的注释,而且非常容易阅读,所以我鼓励你去看看里面的内容。它确实帮助我理解了一些更通用的passport工作方式。
https://stackoverflow.com/questions/20929092
复制相似问题