Friday 18 November 2011

How To Connect To fb From asp.net and get user's info from fb..

Hi frnds.....Today i'm going to tell u abou fb connect from your asp.net
Firstly you’ve to setup a facebook application to get the application id. You can setup application from here. Or if you have already setup a facebook application then just copy the application id and replace with xxxxxxxxxxx.

<div id="fb-root"></div>
        <script type="text/javascript">
            window.fbAsyncInit = function() {
     FB.init({appId: 'xxxxxxxxxxxxxx', 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>
Just put this code in your html file after the body tag.
And your html tag should be 
<!DOCTYPE html>
Now we will use another method FB.Event.subscribe so that we can subscribe some events like login, logout, sessionChange.  So modify the window.fbAsynInit and update by below code
<script type="text/javascript">
window.fbAsyncInit = function() {
 FB.init({appId: 'xxxxxxxxxxx', status: true, cookie: true, xfbml: true});

     /* All the events registered */
     FB.Event.subscribe('auth.login', function(response) {
         // do something with response
         login();
     });
     FB.Event.subscribe('auth.logout', function(response) {
         // do something with response
         logout();
     });

     FB.getLoginStatus(function(response) {
         if (response.session) {
             // logged in and connected user, someone you know
             login();
         }
     });
 };
</script>

So when user will login first time (not automatically)  login() method will call. When user will click logout , logout() method will call. So define these functions for your application purpose. We also use another method FB.getLoginStatus() within window.fbAsyncInit() function so that when we found user is logged in or connected we will show some info to user.
Now we have to render fb connect button so that user can login or logout..
<fb:login-button autologoutlink="true" perms="email,user_birthday"></fb:login-button>
perms=”email,user_birthday,status_update,publish_stream” this line is just for asking user permission for His/Her email,birthday..
2. How to use graph api
To fully understand graph api visit the link . Now i’ll show how you can use graph api using javascript sdk. There is a method FB.api . This method directly hooks graph api and returns result. Sample code
1
2
3
FB.api('/me', function(response) {
  alert(response.name);
});
 Here is full source code:..
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head runat="server">
    <title>FbLogin</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="fb-root">
        </div>

        <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({appId: 'xxxxxxxxxx', status: true, cookie: true, xfbml: true});

                /* All the events registered */
                FB.Event.subscribe('auth.login', function(response) {
                    // do something with response
                    login();
                });
                FB.Event.subscribe('auth.logout', function(response) {
                    // do something with response
                    logout();
                });

                FB.getLoginStatus(function(response) {
                    if (response.session) {
                        // logged in and connected user, someone you know
                        login();
                    }
                });
            };
            (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);
            }());

            function login(){
                FB.api('/me', function(response) {
                    document.getElementById('login').style.display = "block";
                    document.getElementById('login').innerHTML = response.name + " succsessfully logged in!"+"With Email ID"+response.email+"and UserID is"+response.id;
                });
            }
            function logout(){
                document.getElementById('login').style.display = "none";
            }
    
        </script>
 <p><fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button></p>
 <div id="login" style ="display:none"></div>
        <div id="name"></div>
    </div>
    </form>
</body>
</html>


Hope you Enjoyed...Have a grt day:)
 

No comments:

Post a Comment