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?
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..
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.
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 />";
});
});
}
Its good to call graph api call to get User first name. You can also Query User table to get user first name. Its depends upon you what do you like?
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.
精彩评论