使用护照获取有关facebook用户的信息

时间:2015-06-10 18:58:58

标签: node.js facebook-graph-api passport-facebook

这是我在node.js中的第一步,特别是护照,我遇到了一个非常烦人的问题。我试图用他的Facebook个人资料获得用户参加的事件,但无论我尝试它只是没有工作。所以我认为“好吧,让我们获取其他数据”,但除了基本的显示名称和个人资料图片外,任何其他尝试(生日,事件,朋友列表等)最终都没有数据。我在最近几天尝试过使用Facebook的api很多(这是第一次)并且根本无法解决这个问题......这是我的最后一次尝试:

passport.use(new FacebookStrategy({
    clientID: config.fb.appID,
    clientSecret: config.fb.appSecret,
    callbackURL: config.fb.callbackURL,
    profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(accessToken, refershToken, profile, done){
    //Check if the user exists in our Mongo DB database
    //if not, create one and return the profile
    //if exists, return profile
    userModel.findOne({'profileID':profile.id}, function(err, result){
        if(result){
            done(null,result);
        } else {
            // Create a new user in our mongoLab account
            var newFbUSer = new userModel({
                profileID: profile.id,
                fullname: profile.displayName,
                profilePic:profile.photos[0].value || '',
                birthday:profile.birthday,
                //friends:profile.user.friends[0],
                profileUrl:profile.profileUrl

            });


            newFbUSer.save(function(err){
                done(null,newFbUSer);
                console.log(newFbUSer.displayName);
            })
        }
    })
}))  

有关如何获取和使用用户的朋友列表/事件的任何帮助?

2 个答案:

答案 0 :(得分:0)

也许您在调用Facebook登录时未将所需信息的详细信息传递给Facebook。在调用facebook登录时,您需要指定范围内所需的所有信息。例如,如果您需要public_profile,email,user_friends以下是您将在路线中添加的代码:

app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'public_profile,email,user_friends' }));

答案 1 :(得分:0)

请尝试使用全名:

fullName: profile.name.givenName + ' ' + profile.name.familyName
相关问题