Get user basic information using facebook Login button plugin?

Yashwant Kumar Sahu picture Yashwant Kumar Sahu · Aug 20, 2011 · Viewed 22.5k times · Source

Here I am using Facebook Login button plugin and javascript sdk

I am able to successfully login and logout by using above.

When a first time user has gone through authentication process I need to store user basic information i.e. Facebook login name, email in my database.

Please suggest how I can do this.

<p><fb:login-button autologoutlink="true"></fb:login-button></p>


    <div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function () {
            FB.init({ appId: '123456', status: true, cookie: true,
                xfbml: true
            });
        };


        (function () {
            var e = document.createElement('script');
            e.type = 'text/javascript';
            e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
        } ());
    </script>

Answer

Wayne Kao picture Wayne Kao · Aug 20, 2011

Subscribe to the event auth.login. If you do this, Facebook will call your handler after a login as happened.

In that handler, use FB.api to call the Graph API to get any information you desire. For example calling /me as shown in the second example will get you basic information about the logged in user.

Now you have all the data in JavaScript. To send that up to your server, do a plain old XMLHttpRequest/AJAX request. Your JavaScript library probably makes this easy -- in jQuery this is jQuery.ajax() -- but worst case you can use XHR directly.

Now you have the data on your server and you can do whatever you want, like store it in the database. If you only want to store the data once, just check that you haven't already stored info about that user ID yet.