Passport-Facebook authentication is not providing email for all Facebook accounts

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

I am using Passport-Facebook authentication.

   passport.use(new FacebookStrategy({             clientID: 'CLIENT_ID',             clientSecret: 'CLIENT_SECRET',             callbackURL: "http://www.example.com/auth/facebook/callback"         },         function (accessToken, refreshToken, profile, done) {                         process.nextTick(function () {                                console.log(profile)             });                        }     )); 

For some of the Facebook accounts I don't get the email_id and I even tried by using a scope variable such as below, but still I am unable to get the email_id.

profileUrl : " " and ProfileFields : ['','']

回答1:

Make sure these two things are in your code:

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

and this:

app.get('/connect/facebook', passport.authorize('facebook', { scope : ['email'] })); 

This gives you access to the following:

  • profile.id
  • profile.name.givenName
  • profile.name.familyName
  • profile.emails

The last one being an array, so use profile.emails[0].value to get the first email address of the user.

As shamim reza pointed out, you might want to check if profile.emails !== undefined because the property only exists if the user has at least one verified email address.



回答2:

When you are authenticating use something similar to this. You need to use add 'email' in the scope when you authenticate.

app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email']}),     function(req, res){ }); 

That has worked for me.

Here were a few links that helped me out.

https://github.com/jaredhanson/passport-facebook/issues/11 https://github.com/jaredhanson/passport-facebook#how-do-i-ask-a-user-for-additional-permissions



回答3:

I would like to add more information here.

While adding profileFields: ['emails'] while creating FacebookStrategy and passport.authorize('facebook', { scope : ['email'] }) solves issue for most of the users.

There are other possible reasons where you will not get user's email after authentication.

  • No e-mail address on account
  • No confirmed e-mail address on account
  • No verified e-mail address on account
  • User entered a security checkpoint which required them to reconfirm their e-mail address and they have not yet done so
  • User's e-mail address is unreachable

https://developers.facebook.com/bugs/1802930019968631/



回答4:

You can use the code provided as an example in the passport-facebook site as a starting point. Then, to get access to email, make sure to check @Forivin's answer.



回答5:

passport.use(new FacebookStrategy({         clientID: 'CLIENT_ID',         clientSecret: 'CLIENT_SECRET',         callbackURL: "http://www.example.com/auth/facebook/callback"     },     function (accessToken, refreshToken, profile, done) {                     process.nextTick(function () {                            console.log(profile)         });                    } )); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!