Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook returns undefined for email and birthday

I am trying to use the Facebook API that provides first_name, last_name, gender, email, birthday for signup on my website. I used the Graph API and generated an access token with necessary scope. I tried on my personal account in which I created the app and it returned the fields properly, but for FB test user and friend's account, it returns email and birthday as "undefined". In graph.facebook.com it shows the following error for a friend's account.

enter image description here

Facebook returns email id for my friend's same account on doulingo.

Am I setting the permissions incorrectly or is there any other problem ?

like image 454
Pranav Karnik Avatar asked Mar 08 '14 07:03

Pranav Karnik


2 Answers

The problem with my code was all because of not using access_token. I used access_token to retrieve all, first_name, last_name, gender, birthday, email.

function authUser() {
            FB.login(checkLoginStatus, {scope:'email, user_likes'});
            function checkLoginStatus(response) {
                if(!response || response.status !== 'connected') {
                    alert('User is not authorized');
                    document.getElementById('loginButton').style.display = 'block';
                } else {
                    alert('User is authorized');
                    document.getElementById('loginButton').style.display = 'none';
                    console.log('Access Token: ' + response.authResponse.accessToken);

                    //This has to be in your callback!
                    var uid = response.authResponse.userID;
                    var accessToken = response.authResponse.accessToken;
                // access token is very important to get some fields like email, birthday

                    getData();
                }
            }
        }


    function getData() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.' + response.id);
        });
    };
like image 164
Pranav Karnik Avatar answered Sep 18 '22 18:09

Pranav Karnik


check my answer here to see that is a well known issue but not a bug for facebook developers.

Editing.. This is what they say in the facebook developers site:

"...The documentation for the 'email' field of the 'user' object ( https://developers.facebook.com/docs/reference/api/user/ ) clarifies the expected behaviour here, which is: "this field will not be returned if no valid email address is available".

There are a number of circumstances in which you may think a user should have an email address returned but they will not. For privacy and security reasons it is not possible to elaborate on the precise reason any specific user's email address will not be returned so please do not ask. Some possible reasons:

No Email address on account; No confirmed email address on account; No verified email address on account; User entered a security checkpoint which required them to reconfirm their email address and they have not yet done so; Users's email address is unreachable.

You also need the 'email' extended permission, even for users who have a valid, confirmed, reachable email address on file..."

like image 42
Hugo Avatar answered Sep 17 '22 18:09

Hugo