Do I have to make a graph API call via FB.api
to get the current logged in user's first name? Or, is there an easier/faster way?
The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.
The Facebook SDK for JavaScript provides a rich set of client-side functionality that: Enables you to use the Like Button and other Social Plugins on your site. Enables you to use Facebook Login to lower the barrier for people to sign up on your site. Makes it easy to call into Facebook's Graph API.
FB.api('/me', function(response) {
$('#name').html("Welcome " + response.name);
});
That's all you need.
Attributes firstname and lastname are not returned by FB JS SDK API login request anymore. This changed sometime between ver. 2.2 (06/2014) and 2.5 (01/2015).
The reason behind is, that login request only returns publicly available attributes, which is fbid and name. Firstname and lastname are not publicly accessible attributes and require permissions granted by a user. See here.
We use a "guess workaround" - if the attribute name has more than 1 word, we split them into firstname and lastname attributes, or populate firstname from name. It's better/easier in some situations than bothering users with permissions, response checkups, callbacks etc..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With