Tuesday, 15 May 2012

How to implement facebook connect using facebook javascript?

Guys,


######### HTML PART #############
<!---fb:login scope="email" >Login with Facebook</fb:login-->
<input type="button" id="login_button_server" onclick="facebookLogin()" value="Facebook Login"/>
<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId: '7845787548501',
            cookie: true,
            xfbml: true,
            oauth: true
        });
        FB.Event.subscribe('auth.statusChange', function(response) {
            //alert(response.status);
            if (response.status === 'connected') {

                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                FB.api('/me', function(response) {

                    //alert(response);
                    var email = response.email;
                    var name = response.name;
                    var username = response.username;
                    alert('Good to see you, ' + response.email + '.');
                });
            }else{
                alert("Error found while logged in. Please try again.");
            }
        });

    };
    (function () {       
        var e = document.createElement('script');
        e.async = true;
        e.type = 'text/javascript';
        e.src = 'https://connect.facebook.net/en_US/all.js';       
        document.getElementsByTagName('head')[0].appendChild(e);
    }());


    function facebookLogin() {
        FB.login(function(response) {}, {scope:'email'});
    }
</script>

Cheers!

3 comments:

  1. Replies
    1. Hi Clement,

      Yes that's good enough.

      If you required further assistance please let me know.

      Thanks
      Ashwani

      Delete