gpt4 book ai didi

node.js - Facebook Node Passport 不返回电子邮件地址

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:45 26 4
gpt4 key购买 nike

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

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

我将粘贴标准 Passport 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 } }

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

最佳答案

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

来自

passport.authenticate('facebook');

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

然后字段电子邮件将被填充

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

关于node.js - Facebook Node Passport 不返回电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32591771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com