简体   繁体   English

nodejs中的passport-facebook - 个人资料字段

[英]passport-facebook in nodejs - profile fields

some of the profile fields i am trying to get from Facebook when logging in a user, are not going through. 我登录用户时试图从Facebook获取的一些配置文件字段不会通过。

I am using passportjs in node. 我在节点中使用passportjs。 this is the facebook strategy: 这是Facebook的策略:

passport.use(new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: FACEBOOK_CALLBACK_URL,
  profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'email']
},
routes.handleLogin
));

being used with: 正在使用:

app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] }));

the result is that 'link', 'about_me' and 'email' are not getting pulled while the other fields are. 结果是“链接”,“about_me”和“电子邮件”不会被其他字段拉动。

The profileFields parameter adheres to the Portable Contacts convention . profileFields参数遵循Portable Contacts约定 This means you would want to use 'emails' instead of 'email'. 这意味着您需要使用“电子邮件”而不是“电子邮件”。 As for the "about_me" field, it does not appear as if passport-facebook supports the OpenSocial protocol fully. 对于“about_me”字段,看起来好像passport-facebook完全支持OpenSocial协议。 This means you're out of luck if you want to use the "profileFields" parameter for both of these profile elements. 如果您想对这两个配置文件元素使用“profileFields”参数,这意味着您运气不好。 The following code snippet, taken from the master branch, illustrates this limitation: 以下代码片段取自主分支,说明了此限制:

Strategy.prototype._convertProfileFields = function(profileFields) {
    var map = {
    'id':          'id',
    'username':    'username',
    'displayName': 'name',
    'name':       ['last_name', 'first_name', 'middle_name'],
    'gender':      'gender',
    'profileUrl':  'link',
    'emails':      'email',
    'photos':      'picture'
};
...

The fields listed in this mapping are the only ones supported right now. 此映射中列出的字段是目前唯一支持的字段。

Fortunately all is not lost. 幸运的是,一切都没有丢失。 If you choose not to use the profileFields parameter then oddly enough you will be sent the "about_me" content you are interested in via a property called "bio". 如果您选择使用profileFields参数,那么奇怪的是,您将通过名为“bio”的属性向您发送您感兴趣的“about_me”内容。 Here's how you could access that: 以下是您可以访问的方式:

passport.use(new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: FACEBOOK_CALLBACK_URL
},
function(accessToken, refreshToken, profile, done) {
   console.log("bio: " + profile._json.bio);
}));

Unfortunately this doesn't give you the other data you were interested in. I'm guessing that in your situation you are probably looking at gathering the supported convention fields during the passport-facebook callback and then grabbing the extended profile fields in a followup call using the facebook api directly. 不幸的是,这并没有给你感兴趣的其他数据。我猜你在你的情况下你可能正在寻找在passport-facebook回调期间收集支持的约定字段,然后在后续调用中获取扩展的配置文件字段直接使用facebook api。 Either that or poke the passport-facebook maintainers to extend their field support. 无论是那个还是戳护照-facebook维护者来扩展他们的现场支持。

aturkelson is correct. aturkelson是对的。 about_me is not supported yet. about_me尚不支持。 As far as email it comes with the profile as long as you request it. 只要您提出要求,就电子邮件而言,它会附带个人资料。 I also have a console log to confirm I am not crazy. 我还有一个控制台日志,以确认我不是疯了。

//Passport facebook strategy
exports.passportInit= passport.use(new facebookStrategy({
clientID: process.env.FACEBOOK_APP_ID ,
clientSecret: process.env.FACEBOOK_SECRET_ID,
callbackURL: '/api/auth/facebook/callback',
profileFields: ['id', 'displayName', 'emails', 'photos']
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
 db.User.findOne({facebook_id: profile.id}, function(err, oldUser){
     if(oldUser){
         done(null,oldUser);
     }else{
         var newUser = new db.User({
             facebook_id : profile.id,
             facebook_photo : profile.photos[0].value,
             email : profile.emails[0].value,
             display_name : profile.displayName,
             // picture: profile.picture
         }).save(function(err,newUser){
             if(err) throw err;
             done(null, newUser);
         });
     }
 });
}
));

According to my knowledge FB is providing you the info... try this piece of code.. 据我所知,FB正在为您提供信息...试试这段代码..

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: FACEBOOK_CALLBACK_URL,
    profileFields: ['id', 'displayName', 'email'] }, 

         function(accessToken, refreshToken, profile, done) {

         // asynchronous
         process.nextTick(function() {
            FACEBOOK_TOKEN = accessToken;
            FACEBOOK_USER = profile._json;


            // facebook can return multiple emails so we'll take the first
            profile.emails[0].value;

            console.log(FACEBOOK_USER.email);
            done(null, profile);
      });
  }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM