35

I'm trying to get some basic information using Facebook api, but so far I only get the user's name and id. As in { name: "Juan Fuentes", id: "123456" }

I need to get mor einformation, like email, first name, last name and birthday

This is my js code

function facebookLogin() {
  FB.login(function(response) {
    var token = response.authResponse.accessToken;
    var uid = response.authResponse.userID;
    if (response.authResponse) {
      FB.api('/me', 'get', { access_token: token }, function(response) {
        console.log(response);
      });

      FB.api('/'+uid, 'get', { access_token: token }, function(response) {
        console.log(response);
      });
    }
  },
  { scope: 'public_profile' }
  );
}

And this is the button that activates it

<a id="fb-login" href="#" onclick="facebookLogin()"></a>

4 Answers 4

55

You need to manually specify each field since Graph API v2.4:

Declarative Fields
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.

E.g.

FB.api('/me', 'get', { access_token: token, fields: 'id,name,gender' }, function(response) {
    console.log(response);
});
5
  • You could edit answer to activate notification for problems like this developers.facebook.com/settings/developer/contact
    – Thellimist
    Commented Sep 16, 2015 at 16:00
  • 1
    Well facebook did not update the login example on the developer site and waste me several days. :)
    – Dave
    Commented Jan 16, 2017 at 22:00
  • how to send multilevel fields like in feed api i want from scope and in from i want user picture should come? Commented Apr 27, 2017 at 8:02
  • Took me days to figure that one out. I inherited legacy code using Graph API v2.1 that suddenly broke down when FB stopped supporting it on July 10 2017. Could not seek what to upgrade using the API Upgrade Tool because they removed v2.3 and below from it. Commented Jul 25, 2017 at 13:32
  • How would I get the photo album object using the new explicit version from a URL like this: graph.facebook.com{album_id}/photos?access_token={app_id}|{app_secret}
    – Kirk Ross
    Commented Jul 31, 2017 at 16:26
4

It's also possible to use this syntax for data from public_profile scope (tested in Graph API v2.9):

FB.api('/me?fields=birthday,link,gender,age_range', function(response) {
   console.log(response);
});

You can test the possible values online in Graph API Explorer, just click "Get Token" button:

https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Dbirthday%2Clink%2Cgender%2Cage_range&version=v2.9

1

Note that email is not always returned by a call to the me api with email as field, even if scope email was requested and granted, if e.g. the user signed up with a phone number:

https://developers.facebook.com/docs/facebook-login/permissions#reference-email

1
  • Why is this? This is such a weird design. Is there any workaround?
    – tinnick
    Commented Dec 9, 2023 at 19:18
0

Here is the complete Script:

jQuery(document).ready(function () {
    openLoginPopup();
})
function openLoginPopup() {
    FB.getLoginStatus(function (response) {
        if (response.status == 'connected') {
            getCurrentUserInfo(response);
        } else {
            FB.login(function (response) {
                if (response.authResponse) {
                    getCurrentUserInfo(response);
                } else {
                    console.log('Auth cancelled.');
                }
            }, {scope: 'email'});
        }
    });

}

function getCurrentUserInfo() {
    FB.api('/me?fields=id,email,first_name,last_name,name', function (userInfo) {
        console.log(userInfo.name + ': ' + userInfo.email);
    });
}

window.fbAsyncInit = function () {
    FB.init({
//        appId: 'xxxxxxxxxxxxxxxxxxxxx', //livemode
        appId: 'xxxxxxxxxxxx', //testmode
        cookie: true, // Enable cookies to allow the server to access the session.
        xfbml: true, // Parse social plugins on this webpage.
        version: 'v4.0'           // Use this Graph API version for this call.
    });
};


(function (d, s, id) {                      // Load the SDK asynchronously
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id))
        return;
    js = d.createElement(s);
    js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

Thanks.

Not the answer you're looking for? Browse other questions tagged or ask your own question.