The easiest way to get connected on facebook.
/**
* FB Login
*
* @author JoseRobinson.com
* @link GitHup: https://gist.github.com/jrobinsonc/7197898
* @version 201310291828
*/
var FB_login = (function (required_permissions, success_callback, fail_callback) {
function login () {
FB.login(function(response) {
if (response.status === 'connected') {
check_permissions(function() {
fail_callback();
});
} else {
fail_callback();
}
}, {
scope: required_permissions
});
}
function check_permissions (check_callback) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/' + response.authResponse.userID + '/permissions', function(user_permissions) {
var required_permissions_array = required_permissions.split(',');
for (var key in required_permissions_array) {
if ((required_permissions_array[key] in user_permissions.data[0]) === false || user_permissions.data[0][required_permissions_array[key]] !== 1) {
check_callback();
return;
}
}
FB.api('/' + response.authResponse.userID, function(response) {
success_callback(response);
});
});
} else {
check_callback();
}
});
}
check_permissions(function(){
login();
});
});
For use this script you have to complete the followign 2 steps: (1) Create a Facebook app and (2) Add the Facebook SDK for JavaScript, that are described in the followin guide: Getting Started with Facebook Login for Web.
Then, you must include the fb_login.js file and call the function FB_login() with the following 3 params:
permissions
: The permisssions that you want to get from the user's facebook account.callback_success
: The function to execute if the permissions are granted by the user. This function receive one param, the user's info.callback_fail
: The function to execute if the permissions are not granted.Example usage:
// Get the basic info, email, birthday and get permissions to publish on the user's wall.
FB_login('basic_info,user_birthday,email,publish_actions', function(user) {
// Publish on the user's wall.
FB.api(
'me/links',
'post',
{
link: 'http://joserobinson.com/'
},
function(response) {}
);
}, function() {
alert("You must accept the request.");
});
Jose Robinson
www.joserobinson.com
If you get and error or if there are something missing please do a comment.