Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#Javascript
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 9-Dec-12 20:49pm
Comments
AnkitGoel.com at 10-Dec-12 4:42am
   
i think you are missing the call to login() which is required in case of new users.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
<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>
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 756
1 OriginalGriff 452
2 Tadit Dash 350
3 sanket saxena 329
4 Peter Leow 228
0 Sergey Alexandrovich Kryukov 11,890
1 OriginalGriff 7,335
2 Peter Leow 5,003
3 Abhinav S 3,943
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid