Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi experts,

I am writing my first Facebook application using C# SDK.

I am getting required access token from Javascript and then fetching facebook data from using C# server side code.

Above code is working perfectly fine for admin of the facebook application (the user who has created the FB application), but any other user is not getting access token using same code.

I hope my question is clear.

JS code to get access token should work for any user.
Any kind of help is appropriated.

Thanks a lot in advance.

~Amol Borkar
Posted
Comments
AnkitGoel.com 10-Dec-12 4:42am    
i think you are missing the call to login() which is required in case of new users.

1 solution

YES Ankit...
You are correct.
Actually in order to read any user's data from FB, you should have proper permissions and that user should allow you to do so. Then and then only a user access token will be issued to you.

Following is a tested working code for the same.

XML
<body>
    <div id="wrappertop" runat="server" class="wrapper-top">
    </div>
    <div id="likegate_overlaycontents" runat="server" class="overlay-contents">
        <div id="like-messaging">
            <img class="head" src="images/h1_likegate.png" alt="" /><br />
            <img class="subhead" src="images/subhead_likegate.png" alt="" /><br />
        </div>
    </div>
    <form id="form1" runat="server">
    <div id="fb-root">
    </div>
    <div>
        <asp:GridView AutoGenerateColumns="false" AllowPaging="true" PageSize="10" ID="GridView1"
            runat="server" OnPageIn dexChanging="GridView1_PageIndexChanging">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" />
                <asp:BoundField DataField="name" HeaderText="NAME" />
            </Columns>
        </asp:GridView>
    </div>
    <script type="text/javascript">
        // Load the SDK Asynchronously
        (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 = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        } (document));

        // Init the SDK upon load
        window.fbAsyncInit = function () {
            FB.init({
                appId: '144922775656553', // App ID
                //channelUrl: '//' + window.location.hostname + '/channel', // Path to your Channel File
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true  // parse XFBML
            });

            // listen for and handle auth.statusChange events
            FB.Event.subscribe('auth.statusChange', function (response) {
                if (response.authResponse) {
                    // user has auth'd your app and is logged into Facebook
                    FB.api('/me', function (me) {
                        var accessToken = response.authResponse.accessToken;
                        //alert(accessToken);
                        document.getElementById("txtToken").value = accessToken;
                        if (me.name) {
                            document.getElementById('auth-displayname').innerHTML = me.name;
                        }
                    })
                    document.getElementById('auth-loggedout').style.display = 'none';
                    document.getElementById('auth-loggedin').style.display = 'block';
                } else {
                    // user has not auth'd your app, or is not logged into Facebook
                    document.getElementById('auth-loggedout').style.display = 'block';
                    document.getElementById('auth-loggedin').style.display = 'none';
                }
            });
            $("#auth-logoutlink").click(function () { FB.logout(function () { window.location.reload(); }); });
        }
    </script>
    <h1>
        Facebook Login Authentication
    </h1>
    <div id="auth-status">
        <div id="auth-loggedout">
            <div class="fb-login-button" autologoutlink="true" scope="email,user_checkins">
                Login with Facebook</div>
        </div>
        <div id="auth-loggedin" style="display: none">
            Hi, <span id="auth-displayname"></span>(<a href="#" id="auth-logoutlink">logout</a>)
        </div>
    </div>
    <asp:Button ID="btnMyDetails" runat="server" Text="GetMyDetails" OnClick="btnMyDetails_Click" />
    <asp:Button ID="btnFriendDetails" runat="server" Text="GetFriendsDetails" OnClick="btnFriendDetails_Click" />
    <asp:TextBox ID="txtToken" runat="server" CssClass="BLOCK"></asp:TextBox>
    </form>
</body>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900