passport-facebook - cant get about_me and email profile fields

后端 未结 2 1896
滥情空心
滥情空心 2020-12-29 06:14

I am trying to establish a login system for my app using passport-facebook. everything goes well except for the 2 fields that are getting undefined back from the request.

相关标签:
2条回答
  • 2020-12-29 07:03

    In the profileFields you shoul use emails (plular) instead of email (singular):

    profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'emails']
    

    This is noted in the facebook-passport documentation README.md:

    profileFields parameter which specifies a list of fields (named by Portable Contacts convention)

    And you can find Portable Contacts conventions for passportjs here

    0 讨论(0)
  • 2020-12-29 07:19

    Facebook returns some of the default attributes. If you want to access more details about client's profile you would have to declare it under the FacebookStrategy:

    passport.use(new FacebookStrategy({
    
            clientID: "...",
            clientSecret: "...",
            callbackURL: "...",
            profileFields: ['id', '...', '...', 'photos', 'emails']
    
          }, ...
    

    So once you declare the attributes you would like to receive from Facebook, when someone try to log into your system he will be asked to share his photos or emails with you/your app. Once he approve this you can access its values:

    profile.photos[0].value,
    profile.emails[0].value,
    ...
    

    For emails, sometimes it is useful to change:

    passport.authenticate('facebook');
    

    To this:

    passport.authenticate('facebook', { scope: 'email'}));
    
    0 讨论(0)
提交回复
热议问题