无法从passportjs接收sailsjs中的电子邮件地址

时间:2016-06-26 19:07:01

标签: facebook sails.js passport.js passport-facebook

当我拨打以下电话时,我无法从Facebook获取电子邮件地址......

// https://developers.facebook.com/docs/
// https://developers.facebook.com/docs/reference/login/
facebook: function(req, res) {
  passport.authenticate('facebook', { scope : ['email'] }, function(err, user) {
  req.logIn(user, function(err) {
    if (err) {
      return;
    }

    res.redirect('/');
    return;
  });
 })(req, res);
},

这是响应的处理方式,基本上是PassportJS文档代码。

customMiddleware: function(app) {

  passport.use(new FacebookStrategy({
    clientID: "5xxxxxxxxxxxxx6",
    clientSecret: "exxxxxxxxxxxxxxxxxxxxxxxxxxxd9",
    callbackURL: "http://localhost:1337/auth/facebook/callback"
  }, verifyHandler));

  app.use(passport.initialize());
  app.use(passport.session());
}

这是处理程序。

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


var verifyHandler = function(token, tokenSecret, profile, done) {
   process.nextTick(function() {
   sails.log.info(profile);
   Serviceseeker.findOne({uid: profile.id}, function(err, serviceseeker)                {
       if (serviceseeker) { 
          return done(null, serviceseeker); 
       } 
       else {
          var data = {
             provider: profile.provider,
             uid: profile.id,
             name: profile.displayName
          };

          if (profile.emails && profile.emails[0] && profile.emails[0].value) {
             data.email = profile.emails[0].value;
          }
          if (profile.name && profile.name.givenName) {
             data.firstName = profile.name.givenName;
          }
          if (profile.name && profile.name.familyName) {
             data.lastName = profile.name.familyName;
          }

          Serviceseeker.create(data, function(err, serviceseeker) {
             return done(err, serviceseeker);
          });
       }
     });
   });
 };

这就是我得到的回应

info: info: { id: '1xxxxxxxxxxxxxx9',
              username: undefined,
              displayName: 'Txxxx Slxxxxn',
          name:
            { familyName: undefined,
              givenName: undefined,
              middleName: undefined },
          gender: undefined,
          profileUrl: undefined,
          provider: 'facebook',
          _raw: '{"name":"Txxxx Slxxxxn","id":"1xxxxxxxxxxxxxx9"}',
          _json: { name: 'Txxxx Slxxxxn', id: '1xxxxxxxxxxxxxx9' } }

1 个答案:

答案 0 :(得分:0)

你有没有尝试过:

 passport.use(new FacebookStrategy({
        clientID: 'CLIENT_ID',
        clientSecret: 'CLIENT_SECRET',
        callbackURL: "http://www.example.com/auth/facebook/callback"
        passReqToCallback : true,
        profileFields: ['id', 'emails', 'name'] //This
    },