Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook-C#-sdk MVC "Hello World" app - how to get access token?

I've downloaded the C# Facebook SDK "Simple MVC Website Example" from CodePlex at:

http://facebooksdk.codeplex.com/releases/view/54371

and have successfully got it to authenticate to my test Facebook app. But I can't quite work out how to get the access token (and I'm going to want offline access, so I only need to grab that token once, when the user first authorizes my app to grab their feed data).

Many thanks

like image 222
Dave B Avatar asked Nov 19 '10 18:11

Dave B


People also ask

How can I recover my Facebook password without code?

To reset your password if you're not logged in to Facebook: Click Forgot Password?. Type the email, mobile phone number, full name or username associated with your account, then click Search. Follow the on-screen instructions.

What is Fblite?

Facebook Lite is an app by Facebook which includes similar features with less technical requirements. Android users with limited data, poor connections, or older phones can still enjoy using Facebook to connect with friends, family, and businesses through their phone.

Can people see who viewed their Facebook?

No, Facebook doesn't let people track who views their profile. Third-party apps also can't provide this functionality. If you come across an app that claims to offer this ability, please report the app. Was this helpful?


1 Answers

You will want to do two things. First, to request offline_access, you need to change the Javascript login function to request offline access as follows. This is in the Views/Home/Index.aspx.

    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
        FB.init({ appId: '<%:FacebookSettings.Current.AppId %>', status: true, cookie: true, xfbml: true });
        $('#fbLogin').click(function() {
            FB.login(function (response) {
                if (response.session) {
                    window.location = '<%:Url.Action("Profile") %>'
                } else {
                    // user cancelled login
                }
            }, { perms: 'offline_access' });
        });
    </script>
</asp:Content>    

Next, to get the access token, you just do the following in action after the user is authenticated:

    public ActionResult Profile()
    {
        var app = new FacebookApp();
        if (app.Session == null)
        {
            // The user isnt logged in to Facebook
            // send them to the home page
            return RedirectToAction("Index");
        }
    // Read current access token:
        var accessToken = app.Session.AccessToken;

        // Get the user info from the Graph API
        dynamic me = app.Api("/me");
        ViewData["FirstName"] = me.first_name;
        ViewData["LastName"] = me.last_name;

        return View();
    }
like image 56
Nathan Totten Avatar answered Oct 31 '22 20:10

Nathan Totten