【问题标题】:passport-facebook - cant get about_me and email profile fieldspassport-facebook - 无法获取 about_me 和电子邮件个人资料字段
【发布时间】:2013-12-15 23:32:09
【问题描述】:

我正在尝试使用 passport-facebook 为我的应用程序建立登录系统。 一切顺利,除了从请求中返回未定义的 2 个字段。

我将发布我的整个登录过程代码,因为我在这里没有看到很多关于它的信息,即使有很多问题。

这是app.js中的配置

var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;

passport.serializeUser(function(user, done) {
  done(null, user.facebookId);
});
passport.deserializeUser(function(id, done) {
  routes.findUserById(id, function(err, user) {
    done(err, user);
  });
});

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.use(passport.initialize());
app.use(passport.session());

实际请求处理,注意我使用了正确的范围

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

这是我在路由器中的用户创建功能

exports.handleLogin = function(accessToken, refreshToken, profile, done) {
  db.userCatalog.findOne({ facebookId: profile.id }, function(err, existingUser) {
    if (err) {
      return done(err);
    }
    else {
      if(existingUser) {
        console.log('User: ' + existingUser.name + ' found and logged in!');
        done(null, existingUser);
        } 
      else {
        new db.userCatalog({
        name: profile.displayName,
        facebookId: profile.id,
        link: profile.link,
        picture: profile.photos[0].value,
        bio: profile.about_me,
        email: profile.email
        }).save(function (err, data) {
          if (err) {
            return done(err);
          }
          else {
            console.log('New user: ' + data + ' created and logged in!');
            done(null, data);
          }
        });
      }
    }
  });
};

以及完成登录程序后创建新用户时的结果:

我确定这是一些菜鸟的错误,但我自己无法弄清楚......

【问题讨论】:

标签: node.js facebook mongoose passport.js


【解决方案1】:

Facebook 返回一些默认属性。如果您想访问有关客户个人资料的更多详细信息,您必须在FacebookStrategy 下声明它:

passport.use(new FacebookStrategy({

        clientID: "...",
        clientSecret: "...",
        callbackURL: "...",
        profileFields: ['id', '...', '...', 'photos', 'emails']

      }, ...

因此,一旦您声明了希望从 Facebook 接收的属性,当有人尝试登录您的系统时,系统会要求他与您/您的应用分享他的照片或电子邮件。一旦他批准了这个,你就可以访问它的值:

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

对于电子邮件,有时更改是有用的:

passport.authenticate('facebook');

到这里:

passport.authenticate('facebook', { scope: 'email'}));

【讨论】:

  • 完美!效果很好。我发现添加 { scope: 'email' } 是必需的
  • 我们应该使用其他“范围”来获取其他信息吗?
【解决方案2】:

在 profileFields 中,您应该使用电子邮件(复数)而不是电子邮件(单数):

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

这在 facebook-passport 文档README.md 中有说明:

profileFields 参数指定字段列表(按便携式联系人约定命名)

您可以找到针对 passportjs here 的便携式联系人约定

【讨论】:

  • 这是对的,但我还需要将 'about_me' 更改为 'about' 以使代码能够处理并避免错误。
猜你喜欢
  • 2014-04-17
  • 1970-01-01
  • 2013-12-25
  • 2011-03-08
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
相关资源
最近更新 更多