【问题标题】:passport-facebook in nodejs - profile fieldsnodejs中的passport-facebook - 个人资料字段
【发布时间】:2013-12-25 19:23:58
【问题描述】:

我在登录用户时尝试从 Facebook 获取的一些个人资料字段没有通过。

我在节点中使用 passportjs。这是 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
));

用于:

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

结果是 'link'、'about_me' 和 'email' 没有被提取,而其他字段被提取。

【问题讨论】:

    标签: node.js facebook-fql passport-facebook


    【解决方案1】:

    profileFields 参数遵循Portable Contacts convention。这意味着您将要使用“电子邮件”而不是“电子邮件”。至于“about_me”字段,它似乎并不完全支持OpenSocial 协议。这意味着如果您想对这两个配置文件元素都使用“profileFields”参数,那么您就不走运了。以下来自 master 分支的代码 sn-p 说明了这种限制:

    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'
    };
    ...
    

    此映射中列出的字段是目前唯一支持的字段。

    幸运的是,一切都没有丢失。如果您选择 使用 profileFields 参数,那么奇怪的是,您将通过名为“bio”的属性向您发送您感兴趣的“about_me”内容。您可以通过以下方式访问它:

    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);
    }));
    

    不幸的是,这并没有为您提供您感兴趣的其他数据。我猜在您的情况下,您可能正在考虑在 passport-facebook 回调期间收集支持的约定字段,然后在直接使用 facebook api 的后续电话。要么这样做,要么戳护照-facebook 维护人员以扩展他们的现场支持。

    【讨论】:

      【解决方案2】:

      turkelson 是正确的。 about_me 尚不支持。就电子邮件而言,只要您提出要求,它就会随个人资料一起提供。我还有一个控制台日志来确认我没有疯。

      //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);
               });
           }
       });
      }
      ));
      

      【讨论】:

        【解决方案3】:

        据我所知,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);
              });
          }));
        

        【讨论】:

        • 令人困惑的是 profileFieldsemailsemail 视为同一件事......但两者似乎都有效
        猜你喜欢
        • 2013-12-15
        • 2014-04-17
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多