[英]Not getting back email from Facebook OAuth API, Passport with MEAN Stack
我正在努力為用戶登錄我的網站設置Face book OAuth。 我從Facebook獲得了一個帶有我的FB信息的對象,但在該個人資料回復中沒有任何電子郵件的跡象。
當我點擊我頁面上的“登錄Facebook”按鈕時,它將我帶到Facebook並說該應用程序將需要我的個人資料和電子郵件,因為我添加了電子郵件到我想要的范圍。 這是我的代碼:
配置/ routes.js:
app.get('/auth/facebook', passport.authenticate('facebook', {scope:['email']}));
app.get('/auth/facebook/callback',
passport.authenticate('facebook',
{ successRedirect: '/confirm_community',
failureRedirect: '/signin' }));
============== passport.js
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID : configAuth.facebookAuth.clientID,
clientSecret : configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function() {
console.log(profile);
// console.log(profile.emails[0].value);
User.findOne({'facebook.id':profile.id}, function (error, result){
if(error){
return done(error);
}
if(user) {
return done(null, user);
}
else {
var newUser = new User();
newUser.facebook.id = profile.id;
newUser.facebook.token = accessToken;
newUser.facebook.name = profile.displayName;
newUser.facebook.email = profile.emails[0].value;
newUser.save(function (error){
if(error){
console.log(error);
throw error;
} else {
return done(null, newUser);
}
});
}
});
});
}));
這是我在使用FB的響應的process.nextTick函數中的console.log(profile)時得到的FB對象:
{ id: 'my id is coming back here',
username: undefined, (because I don't have one)
displayName: 'my name is coming back here',
name:
{ familyName: undefined, (I don't have any of these in my profile)
givenName: undefined,
middleName: undefined },
gender: undefined,
profileUrl: undefined,
provider: 'facebook',
_raw: '{"name":"my name is coming here","id":"my id is coming here"}',
_json: { name: 'my name is coming here', id: 'my id is coming here' } }
我確保我的電子郵件是公開的,並且通常使用我的電子郵件和密碼登錄到Facebook,所以我沒有使用手機登錄Facebook。 我只是錯過了那個電子郵件地址。 我似乎無法弄清楚為什么那個對象沒有給我回電子郵件? 我錯過了什么? 非常感謝幫助。
我找到了PASSPORT-FACEBOOK節點模塊的另一個解決方案。 希望這會有助於他人。
passport.use(new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['email']
參考: https : //github.com/jaredhanson/passport-facebook/issues/129
在更改日志中搜索“聲明字段”: https : //developers.facebook.com/docs/apps/changelog#v2_4
您現在必須在API調用中指定要獲取的字段: /me?fields=name,email
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.