56

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 : ['','']

Forivin
  • 14,780
  • 27
  • 106
  • 199
AMT
  • 701
  • 2
  • 6
  • 7
  • Not every FB account _has_ an email address set … (People are able to sign up for FB using their mobile number as well.) – CBroe Apr 05 '14 at 12:55
  • but i am not getting email from accounts which have email and to get registered to fb we need to have email id right?? – AMT Apr 06 '14 at 18:19
  • I don’t see you ask for `email` permission anywhere … you said you tried to use scope, but where? And no, people do not have to give an email address to register for FB, as I already said. – CBroe Apr 06 '14 at 18:40
  • Make sure to log `accessToken` there and try it with the [Graph API Explorer](https://developers.facebook.com/tools/explorer/). Make sure that the "email" permission on the left is *not greyed out*. If it is, you're not asking for the scope at the right time. See [this comment](https://github.com/jaredhanson/passport-facebook/issues/214#issuecomment-348684429) on the passport-facebook project. – a paid nerd Dec 02 '17 at 21:01
  • https://stackoverflow.com/questions/21775198/facebook-api-this-app-is-in-development-mode This solves my issue. Try this. – NOOBIEBOY97 Sep 21 '21 at 16:11

7 Answers7

135

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.

As Weft pointed out, you might have to use the property email instead of emails.

Forivin
  • 14,780
  • 27
  • 106
  • 199
26

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

Vinnie James
  • 5,763
  • 6
  • 43
  • 52
sifxtreme
  • 425
  • 5
  • 8
  • 1
    Make sure to include the 'profileFields' field when setting up your strategy. Only adding 'scope' in here won't do the trick (at least didn't for me). For more info, look the other answer: http://stackoverflow.com/a/32370813/1814970. – marcelocra Jan 01 '17 at 14:22
  • I am getting emails for some users but not for some users ? Any reasons for that ? – Sandip Subedi Apr 09 '17 at 03:22
  • 1
    @SandipSubedi it seems like that question is answered by vinesh below – sifxtreme Apr 13 '17 at 14:58
8

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

​ you need to make sure that your user does not have any of the issues listed above. more information can be found https://developers.facebook.com/bugs/1802930019968631/

vinesh
  • 4,745
  • 6
  • 41
  • 45
3

When passport doesn't return the profile.emails, profile.name.givenName, profile.name.familyName fields, or if they are missing, you can try to parse the https://graph.facebook.com/v3.2/ url, although you still need a token. You access the url, with of course a valid token, like:

https://graph.facebook.com/v3.2/me?fields=id,name,email,first_name,last_name&access_token=

It outputs a JSON response like:

{
   "id": "5623154876271033",
   "name": "Kurt Van den Branden",
   "email": "kurt.vdb\u0040example.com",
   "first_name": "Kurt",
   "last_name": "Van den Branden"
}

Install the request module ($ npm install request --save), to be able to parse a JSON url and in your passport.js file:

const request = require("request");

passport.use(new FacebookStrategy({
        clientID        : 'CLIENT_ID',
        clientSecret    : 'CLIENT_SECRET',
        callbackURL     : "https://example.com/auth/facebook/callback"
    },
    function(req, token, profile, done) {

        let url = "https://graph.facebook.com/v3.2/me?" +
                  "fields=id,name,email,first_name,last_name&access_token=" + token;

        request({
            url: url,
            json: true
        }, function (err, response, body) {
              let email = body.email;  // body.email contains your email
              console.log(body); 
        });
    }
));

You can add a lot of other parameters to the url, although some of them require user permission to return values. You can play with it on: https://developers.facebook.com/tools/explorer/

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
3

Make sure you're providing the scope parameter into the first .authenticate() call, not the callback.

Like this:

router.get("/auth/facebook", passport.authenticate("facebook", {
    scope: [ "email" ], // scope goes here, not below
}));

router.get("/auth/facebook/callback",
    passport.authenticate("facebook", {
        successRedirect: "/",
        failureRedirect: "/login",
    }),
);
LachoTomov
  • 3,312
  • 30
  • 42
-1

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.

Community
  • 1
  • 1
marcelocra
  • 2,094
  • 2
  • 24
  • 37
-3
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)
        });               
    }
));
Tunaki
  • 132,869
  • 46
  • 340
  • 423