1

大家好,我也在这几个地方找过

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

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

4

2 回答 2

1

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

passport.authenticate('facebook');

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

然后将填充字段电子邮件

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

于 2015-09-15T17:16:00.797 回答
0

我的配置是相同的,除了 profileFields 我使用emails(复数)而不是email

// 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);
于 2016-10-09T23:35:45.243 回答