Facebook Graph API won't return email address

JavascriptFacebook

Javascript Problem Overview


Edit: Not Duplicate, because:

  • I have the permission
  • Debugged the token
  • Code works with test user

Please don't mark as duplicate without reading.

I'm trying to get the user e-mail address, but i don't get it. On graph api explorer, when i hit send, email field becomes grayed and says that:

> field is empty or disallowed by access token

But when I debug the token it has email permission granted

My profile has an e-mail address.

Update: I tried https://developers.facebook.com/tools/console/ . My profile returns nothing, even on another computer. But the same code returns the email, name and uid of another account.

Code:

    <fb:login-button scope="email">
  Grant Permissions to make more examples work
</fb:login-button>

<button onclick="doPost()">Post to Stream</button>

<script>
function userData() {
  FB.api('/me?fields=name,email', Log.info.bind('/me callback'));
};

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    userData();
  }
});
</script>

It is possible to lockdown you e-mail so no one can has it? Even when i grant permission?

Javascript Solutions


Solution 1 - Javascript

The Marcus' answer leads me to the real problem I was facing.

Debugging with the Facebook Explorer tool I was able to figure out that I should set the email scope at the FB.api() method after a successful login.

FB.login(function(response) {
    if (response.status === 'connected'){
        FB.api('/me?fields=id,email,name', function(data) {
        console.log( data.email ) // it will not be null ;)
    })
}, {scope: 'email'});

It is not quite clear at the Quickstart guide and I hope it helps someone else, too.

Solution 2 - Javascript

I had the same problem and I think I found out why: If the user has an unconfirmed email in Facebook (i.e. Facebook sent him a validation mail to his email address but he didn't respond) Facebook WILL NOT pass that email to your app even if he gave you the email permissions (!!!).

So what I did is use his Facebook email if he has a user name (i.e. [email protected]).

Solution 3 - Javascript

After i got my bug report marked as duplicate, and i read all posts and links there, i got what caused this problem for me and how to fix.

The Problem Facebook seems to sometimes forget what your primary e-mail is on the graph API (But it still there in the preferences.)

Solution The user affected must remove the e-mail, save settings, then re-add the address, re-confirm, then make it primary. This fixed my account both on my sandbox app, and other apps where Facebook login don't used to work.

Solution 4 - Javascript

New facebook graph requires scopes added in the /me request as follow:

/me?fields=email,birthday,location,locale,age_range,currency,first_name,last_name,name_format,gender&access_token=

Solution 5 - Javascript

I had the same issue while I was developing the fb login button for my site. I had even setup permissions for my app here:

https://developers.facebook.com/apps/<my-app-ID>/permissions

and it was working fine for certain initial cases, that is, it was giving email (i tested it on my own account and it was giving my email). Then suddenly it started to reflect no email at all. After two hours of browsing, I figured it out that there was an issue with the access token as when I went on this link:

https://developers.facebook.com/tools/explorer/<your-fb-id>/?method=GET&path=100002451127858%3Ffields%3Did%2Cemail

Update your-fb-id with your id and go to the above link. Click on 'Get Access Token'. In the tab that opens up, click on 'Extended Permissions' and in that, choose 'email' and submit. Now, test your query again. It'll definitely work, on the console as well as your website. Cheers! :)

Solution 6 - Javascript

This is a known bug. If the user does not have any email address set to primary, the query will return null for email. Set the email address for your account to primary https://www.facebook.com/settings?tab=account&section=email&view and then try.

Source: https://developers.facebook.com/bugs/298946933534016/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjoao BenoView Question on Stackoverflow
Solution 1 - JavascriptMiereView Answer on Stackoverflow
Solution 2 - JavascriptozbaView Answer on Stackoverflow
Solution 3 - Javascriptjoao BenoView Answer on Stackoverflow
Solution 4 - JavascriptAlin RazvanView Answer on Stackoverflow
Solution 5 - JavascriptSayedView Answer on Stackoverflow
Solution 6 - Javascriptspence0021View Answer on Stackoverflow