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 21: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));
 
<pre>
    // 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(&quot;txtToken&quot;).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';
            }
        });
        $(&quot;#auth-logoutlink&quot;).click(function () { FB.logout(function () { window.location.reload(); }); });
    }
&lt;/script&gt;
&lt;h1&gt;
    Facebook Login Authentication
&lt;/h1&gt;
&lt;div id=&quot;auth-status&quot;&gt;
    &lt;div id=&quot;auth-loggedout&quot;&gt;
        &lt;div class=&quot;fb-login-button&quot; autologoutlink=&quot;true&quot; scope=&quot;email,user_checkins&quot;&gt;
            Login with Facebook&lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;auth-loggedin&quot; style=&quot;display: none&quot;&gt;
        Hi, &lt;span id=&quot;auth-displayname&quot;&gt;&lt;/span&gt;(&lt;a href=&quot;#&quot; id=&quot;auth-logoutlink&quot;&gt;logout&lt;/a&gt;)
    &lt;/div&gt;
&lt;/div&gt;
&lt;asp:Button ID=&quot;btnMyDetails&quot; runat=&quot;server&quot; Text=&quot;GetMyDetails&quot; OnClick=&quot;btnMyDetails_Click&quot; /&gt;
&lt;asp:Button ID=&quot;btnFriendDetails&quot; runat=&quot;server&quot; Text=&quot;GetFriendsDetails&quot; OnClick=&quot;btnFriendDetails_Click&quot; /&gt;
&lt;asp:TextBox ID=&quot;txtToken&quot; runat=&quot;server&quot; CssClass=&quot;BLOCK&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/form&gt;

</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 OriginalGriff 638
1 Sergey Alexandrovich Kryukov 445
2 ProgramFOX 275
3 CHill60 225
4 Maciej Los 215
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100