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

后端 未结 2 771
梦毁少年i
梦毁少年i 2021-02-03 12:17

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

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

and have successfully g

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 13:05

    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.

        

    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();
        }
    

提交回复
热议问题