7

I am trying to return user email address using facebook api. Few days ago it was working fine, but today stopped working, and don't know why.

So first I get access token:

FB.login(response => {
    if (response.authResponse) {
      resolve(response.authResponse.accessToken);
    } else {
      reject({
        error: 'User cancelled login or did not fully authorize.'
      });
    }
  }, {
    scope: 'public_profile,email'
  });

then I try to get user data

FB.api('/me', {fields: 'first_name,last_name,email'}, function(response) {
  console.log(response);
});

but I only get data for first_name and last_name. There is no email.

Also when I ask for permissions, it gives me email as granted, but as I said, email is not returned.

FB.api('/me/permissions', function(response) {
  console.log(response);
});

Does anyone knows what can be the problem?

BTW: I am using API v2.4.

user232343
  • 2,008
  • 5
  • 22
  • 34
  • Try the request in Graph API Explorer first, using your app id and the same access token your JS is using (should be contained in the `response` object, so log it to console and copy&paste it from there.) Also, make sure the user has a valid email address set in their account, and that it is verified. – CBroe Oct 18 '15 at 11:25
  • Is this happening for all your users, or just one or a few? Like CBroe mentions; it could be that somebody removed their email from their profile. – Roemer Oct 27 '15 at 11:29

8 Answers8

20
FB.api('/me?fields=email,name', function(response) { /* stuff here */ });

Try adding your return fields to your request.

stillatmylinux
  • 1,399
  • 13
  • 25
  • 1
    that´s not it, the recommended way would be an additional parameter: https://developers.facebook.com/docs/javascript/reference/FB.api - also, he gets the other fields, only email is missing. – andyrandy Mar 02 '16 at 11:09
  • Yes definately email is missing any solution please – Heemanshu Bhalla Jul 03 '16 at 16:16
  • This solution worked for me, passing fields as a parameter was inconsistent. – Ashraful Alam May 29 '17 at 12:25
  • i also had to do this. I specified the proper scope, but the bare '/me' was only returning id,name by default. – Jeff Jul 23 '17 at 18:55
  • In 2020, I had to do this too. Facebook docs are dodgy, fb don't bother to make sure it works as documented, or not consistent across all docs. – joedotnot Jun 13 '20 at 17:34
5

I know this is old question however if someone still seraching for answers then following workaround worked for me.

You need to add email in scope scope="public_profile,email".

<fb:login-button  scope="public_profile,email" autologoutlink="false" onlogin="OnRequestPermission();"></fb:login-button>
N Nem
  • 743
  • 8
  • 14
4

A possible cause is that the user did not validate the email. Try with another user with validated email to make sure this is not the problem.

See also this answer

Community
  • 1
  • 1
lex82
  • 11,173
  • 2
  • 44
  • 69
2

If you set all code correctly and still don't get an email from API. The user probably doesn't have an email bound to the account or the email not confirmed by the user.

Yauhen
  • 401
  • 6
  • 7
0

I made it work using a promise and adding {scope:'email'} to the initial request then including 'email' in the fb.api fields.

fb.login({scope:'email'}).then(function (resp) {
    if (resp.status === 'connected') {
       fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (FbUserData) {
          console.log(FbUserData);
       });
    }
});
Pat M
  • 5,966
  • 7
  • 24
  • 34
0

I spent a lot of time trying to figure out why the email is not returned on the FacebookLoginASPnetWebForms Asp.Net application (https://github.com/nickpinheiro/FacebookLoginASPnetWebForms).

I got it working, thanks to N Nem's post above (thanks!).

Open Default.aspx.cs

In HyperLink1.NaviagteUrl, add the query string parameter: &scope=public_profile,email

HyperLink1.NavigateUrl = "https://www.facebook.com/v2.4/dialog/oauth/?client_id=" + ConfigurationManager.AppSettings["FacebookAppId"] + "&redirect_uri=http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/account/user.aspx&response_type=code&state=1&scope=public_profile,email";

I also had to fix some bugs to parse the access token correctly.

This time, the email address is available! I hope this helps?

0

N Nem above fixed for me. Had the code correct, but missing the scope on the login button. Code I use is below.

function statusChangeCallback(response){
    if(response.status === 'connected'){
        registerUser(response);
    }
}

function checkLoginState() {
  FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
  });
}

function registerUser(statusResponse){
    let userId = statusResponse.authResponse.userID;
    FB.api('/'+userId+'/?fields=id,email,first_name,last_name',function(response){
        if(response && !response.error){
            console.log(response);
        }
    });
}
AdamVanBuskirk
  • 509
  • 4
  • 10
0

This is kinda old post but I am sharing the solution that worked for me.

FB Login SDK version: v6.0

First, make sure that the user (which is you for testing purposes) has a valid/verified email. Refer to the code below.

FB.login(response => {
    // your code
}, {
    scope: 'public_profile,email',
    return_scopes: true,
    auth_type: 'rerequest'
});

And in getting the user details

const response = await axios({
      method: 'get',
      url: 'https://graph.facebook.com/v6.0/me?fields=id,name,email&access_token=ACCESS_TOKEN&pretty=0&sdk=joey&suppress_http_code=1'

});

I've added the parameter fields=id,name,email

Rye
  • 445
  • 4
  • 15