facebook node passport不会返回电子邮件地址

时间:2015-09-15 17:05:44

标签: node.js email passport.js facebook-login passport-facebook

大家好,我一直在这里找几个地方

134尝试验证用户时未返回电子邮件密钥 https://github.com/jaredhanson/passport-facebook/issues/134

我将粘贴标准护照Facebook代码。 我也指定了配置文件字段。问题是FB的请求没有提取用户的电子邮件地址,这对我的应用程序至关重要。

// Load the module dependencies
var passport = require('passport'),
    url = require('url'),
    FacebookStrategy = require('passport-facebook').Strategy,
    config = require('../config'),
    users = require('../../app/controllers/users.server.controller');

// Create the Facebook strategy configuration method
module.exports = function() {
    // Use the Passport's Facebook strategy 
    passport.use(new FacebookStrategy({
            clientID: config.facebook.clientID,
            clientSecret: config.facebook.clientSecret,
            callbackURL: config.facebook.callbackURL,
            profileFields: ["id", "birthday", "email", "first_name", "gender", "last_name"],
            passReqToCallback: true
        },
        function(req, accessToken, refreshToken, profile, done) {
            // Set the user's provider data and include tokens
            var providerData = profile._json;
            providerData.accessToken = accessToken;
            providerData.refreshToken = refreshToken;

            console.log(profile);
            var email = profile.emails ? profile.emails[0].value : 'karl.something@something.com';

            // Create the user OAuth profile
            var providerUserProfile = {
                firstName: profile.name.givenName,
                lastName: profile.name.familyName,
                fullName: this.firstName + ' ' + this.lastName,
                email: email,
                username: profile.username,
                provider: 'facebook',
                providerId: profile.id,
                providerData: providerData
            };

            // Save the user OAuth profile
            users.saveOAuthUserProfile(req, providerUserProfile, done);
        }
    ));
};

在这里你可以看到console.log(profile)输出:

{ id: '894894191844',
username: undefined,
displayName: undefined,
name: { familyName: 'Something', givenName: 'Karl', middleName: undefined },
gender: 'male',
profileUrl: undefined,
provider: 'facebook',
_raw: '{"id":"1197051993654521","first_name":"Karl","gender":"male","last_name":"Something"}',
_json:
{ id: '1197051993654521',
first_name: 'Karl',
gender: 'male',
last_name: 'Koks',
accessToken: 'XYZ',
refreshToken: undefined } }

任何具体的想法,想尽快解决这个问题: - )

2 个答案:

答案 0 :(得分:1)

可能不是唯一的解决方案,但以下对我有用:

来自

passport.authenticate('facebook');

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

然后将填充字段电子邮件

在此处找到答案: passport-facebook - cant get about_me and email profile fields

答案 1 :(得分:0)

我的配置完全相同,除了在profileFields中我使用电子邮件(复数)而不是电子邮件

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

// configure passport
passport.use(facebook(callback));

// configure strategy
export default (callback) => new FacebookStrategy({
  clientID: config.auth.facebook.clientID,
  clientSecret: config.auth.facebook.clientSecret,
  callbackURL: config.auth.facebook.callbackURL,
  passReqToCallback: true,
  enableProof: true,
  profileFields: ['id', 'emails', 'name', 'gender', 'displayName']
}, callback);