Login with Facebook example.


Below is the code for logging our application using Facebook.

Note: Please change appId : '45366235237356756' with your Facebook application ID.


 <div id="fb-root"></div>  

 <script>  
      //Load the Facebook JS SDK  
      (function(d) {  
           var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];  
           if (d.getElementById(id)) {  
                return;  
           }  
           js = d.createElement('script');  
           js.id = id;  
           js.async = true;  
           js.src = "http://connect.facebook.net/en_US/all.js";  
           ref.parentNode.insertBefore(js, ref);  
      }(document));  
      // Init the SDK upon load  
      window.fbAsyncInit = function() {  
           FB.init({  
                appId : '45366235237356756', // App ID  
                status : true, // check login status  
                cookie : true, // enable cookies to allow the server to access the session  
                xfbml : true, // parse XFBML  
                version : 'v2.3'  
           });  
      };  
      function showDetails() {  
           // Specify the user fields to query the OpenGraph for.  
           // Some values are dependent on the user granting certain permissions  
           var fields = [ 'id', 'name', 'first_name', 'middle_name', 'last_name',  
                     'gender', 'timezone', 'birthday', 'email', 'picture', 'location' ].join(',');  
           FB.api('/me', {  
                fields : fields  
           }, function(details) {  
                document.getElementById('fb-root').innerHTML = "Facebook Responce-> \n" + JSON.stringify(details, null, '\t');  
           });  
      }  
      function login(){  
           // Specify the extended permissions needed to view user data  
           // The user will be asked to grant these permissions to the app (so only pick those that are needed)  
           var permissions = [ 'email', 'user_birthday', 'user_hometown', 'user_location'].join(',');  
           // initiate OAuth Login  
           FB.login(function(response) {  
                // if login was successful, execute the following code  
                if (response.authResponse) {  
                     showDetails();  
                }  
           }, {  
                scope : permissions  
           });  
      };  
 </script>  


 <button id="fb-login" onclick="login();">FB Login</button>  

Comments

Popular posts from this blog

How to get context path in JQuery?

How to extend Liferay 6.2 session timeout by AJAX call?