Warm tip: This article is reproduced from stackoverflow.com, please click
email facebook-graph-api jquery scope facebook-access-token

Facebook returns undefined for email and birthday

发布于 2020-04-23 10:26:03

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 ?

Questioner
Pranav Karnik
Viewed
73
Pranav Karnik 2014-03-23 00:02

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);
        });
    };