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?
6 Answers
FB.api('/me', function(response) {
$('#name').html("Welcome " + response.name);
});
That's all you need.
-
6If you want the first name, it should be
response.first_name
, notresponse.name
– ArnaudCommented Jan 4, 2013 at 10:41 -
4@Super Chafouin thanks but response.first_name gived undefined. Commented Jul 28, 2015 at 11:35
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..
Yes you must use the api call, as it's not encoded in the access token or any other information passed to an application.
If the concern is performance, you can of course cache the result.
-
-
window.localStorage.setItem('facebook', JSON.stringify(response));
– AlastairCommented Aug 5, 2013 at 6:04
You can use the FB.api as follows
function fqlQuery(){
FB.api('/me', function(response) {
var query = FB.Data.query('select name,email,hometown_location, sex, pic_square from user where uid={0}', response.id);
query.wait(function(rows) {
uid = rows[0].uid;
document.getElementById('name').innerHTML =
'Your name: ' + rows[0].name + "<br />" +
'Your email: ' + rows[0].email + "<br />" +
'Your hometown_location: ' + rows[0].hometown_location + "<br />" +
'Your sex: ' + rows[0].sex + "<br />" +
'Your uid: ' + rows[0].uid + "<br />" +
'<img src="' + rows[0].pic_square + '" alt="" />' + "<br />";
});
});
}
The javascript SDK that Facebook has allows you to get the first name, last name (which don't require special permission), educational history and a lot more that a Facebook user profile has. But, to get 'sensitive' information the user has to approve your app (if they have not already) and then approve the 'sensitive' information like educational history. The page at http://www.virtualsecrets.com/facebook-user-data-on-webpage-form.html shows the whole process of working with the javascript SDK and some info on how to set-up apps with webpages.