This post shows the basic way of using Facebook SDK C#.Net in Desktop/Windows based application to authenticate user and enable developers to access the complete data of the logged in user.
First of all developer has to create an application in http://www.facebook.com/developers. This is very simple and can be done in seconds. Following are some basic steps.
1. Login http://www.facebook.com/developers
2. Click on a link named as “Set Up New App”.
3. Specify Application Name, in my case its “Test”. Click on ‘Create App”.
4. Once the application is created select the “Website” tab and note the “Application Key”. This Application Key will be used to connect to the Application using Facebook SDK.
Now we have configured and created an application and perform different operations from our desktop application. Following are the steps to develop a basic desktop application that authenticate user and populates his full name and gender in the text fields.
First of all download the provided at CodePlex forum.
1. Open Visual Studio
2. Create Windows Application
3. Download the Facebook Developer SDK from Facebook Developer SDK and add References to the Facebook Developer Kit, through NuGet (gives you an option to reference third party library packages) or add Facebook.dll and Facebook.WinForms.dll manually.
4. Open Visual Studio and Create a new tab in Toolbox section e.g. “Facebook Control” and click on “Choose Items” and browse the “Facebook.Winforms.dll” file.
5. On selection, It will show the list of controls embedded in that assembly, just click on Ok to add it in your IDE toolbox section.
6. Now drag and drop the FacebookService as shown below.
7. Add a button that performs Authentication first, and then get the User details. Also place two text boxes.
8. Now set the Application Key in the Application Key property of FacebookService control as shown below.
9. Now in the button click event place following snippet that connects to facebook and displays the username and email address on the form.
txtUserName.Text = facebookService1.Users.GetInfo().first_name + " " + facebookService1.Users.GetInfo().last_name ;
txtGender.Text = facebookService1.Users.GetInfo().sex;
10. Now when run the application and click on the “Connect to Facebook” button it will show the Facebook login screen, enter valid username and password.
11. On Authentication, it will prompt to allow/don’t allow to use Facebook from Test application. Click Allow.
12. Then it will display the user name and gender of the logged in user.
You can also get friend list, update status, retrieve photos, and bunch of more operation that are provided in the SDK.