Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am really in need of some urgent help please. I am trying to get a new users details from Facebook in C# MVC. I downloaded the NuGet package in Visual Studio and put the code below in my controller.

My questions are:

1. How would I get the code(access token) in C#? Is there a way I can generate one? All the examples I see don't mention how you get it but they just show the code as a parameter in the method.

2. For testing, I created an access token on the Facebook website to see if the user details comes through correctly. I get an error doing this, saying the invalid URL. I searched on Google and I see its because of the localhost url, does anyone know a way around this?

Thanks For all the help.

C#
public ActionResult FacebookCallback(string EmailAddress, string Password, string code)
       {

           var fb = new FacebookClient();

           dynamic result = fb.Post("oauth/access_token", new
           {
               client_id = "XXXXXXXXXXX",
               client_secret = "YYYYYYYYYYYYYYYYYYY",
               redirect_uri = "http%3a%2f%2flocalhost%3a43568%2fRegister",
               code = <big>code</big>
           });

           var accessToken = result.access_token;

           // Store the access token in the session for farther use
           Session["AccessToken"] = accessToken;

           // update the facebook client with the access token so
           // we can make requests on behalf of the user
           fb.AccessToken = accessToken;

           // Get the user's information, like email, first name, middle name etc
           dynamic me = fb.Get("me?fields=first_name,middle_name,last_name,id,email");
           string email = me.email;
           string firstname = me.first_name;
           string middlename = me.middle_name;
           string lastname = me.last_name;

           // Set the auth cookie
           FormsAuthentication.SetAuthCookie(email, false);
           return RedirectToAction("Index", "Home");
       }
Posted

1 solution

 
Share this answer
 
v2
Comments
Ekona_Pikin 11-Mar-15 15:21pm    
Awesome Richy:)
mou7866 12-Mar-15 14:31pm    
Thanks guys, these posts helped a lot

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