Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook JS SDK how to get current user's first name?

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?

like image 608
ma11hew28 Avatar asked Feb 13 '11 23:02

ma11hew28


People also ask

How do I get my Facebook API User ID?

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.

What is Facebook SDK for JavaScript?

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.


2 Answers

FB.api('/me', function(response) {
    $('#name').html("Welcome " + response.name);
});

That's all you need.

like image 102
Mohammad Arif Avatar answered Sep 30 '22 16:09

Mohammad Arif


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..

like image 41
lubosdz Avatar answered Sep 30 '22 16:09

lubosdz