Tuesday 26 May 2015

Facebook SDK javascript - Facebook Login Check, Get Login Details, Share URL and POST to Profile

Facebook SDK javascript - Facebook Login Check, Get Login Details, Share URL and POST to Profile

Add following javascript code which are Responsible for load javascript SDK.
window.fbAsyncInit = function() {
    FB.init({
        appId      : FB_APP_ID, 
        cookie     : true,
        xfbml      : true,                    
        version    : 'v2.3'
    });

}; 
(function(d){
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    d.getElementsByTagName('head')[0].appendChild(js);
}(document));
Please don't forget to replace the "FB_APP_ID" with  facebook API Id.


Add following javascript code which will leverage the functionality like Login check, login details, Share URL and Post to facebook.
function fbLogin(){
    FB.getLoginStatus(function(response) {
     if (response.status === 'connected') {
       console.log('You are login in FB.');
     }
     else {
       FB.login();
     }
   });

}
function fbLoginDetails(){                
    FB.api('/v2.2/me?fields=name,email,location,gender,birthday', function(response) {                   
        console.log(response.toSource());                    
    });
}

function fbShareURL(){
    FB.ui(
        {
         method: 'share',
         href: 'http://www.web-technology-experts-notes.in/2013/09/Facebook-Registration-Plugin-with-Custom-Fields.html'
       }, function(response){   }); 
}

function fbPostURL(){
    FB.ui({
        method: 'share_open_graph',
        action_type: 'og.likes',
        action_properties: JSON.stringify({
         object:'http://www.web-technology-experts-notes.in/2013/09/Facebook-Registration-Plugin-with-Custom-Fields.html',
        })
       }, function(){})

}


Add following HTML code which will create button on which click functionality will work.
<a class="button-facebook" href="javascript:fbLogin()">Facebook Login Check</a>
        <a class="button-facebook" href="javascript:fbLoginDetails()">Facebook Get Login Details</a>

        <a class="button-facebook" href="javascript:fbShareURL()">Facebook Share URL</a>
        <a class="button-facebook" href="javascript:fbPostURL()">Facebook POST URL</a>