Using Facebook SDK API in .Net

Using Facebook SDK API in .Net

Added by Nasir Mahmood updated on Saturday, December 13, 2014

Problem:

Many times we need to use Facebook in .net application. There are many ways to achieve this one of them is by using Facebook SDK API for C#. This is a step by step tutorial How to use Facebook SDK API in .Net.

Facebook:

From Wiki The Facebook Platform is a software environment provided by the social networking service Facebook for third-party developers to create their own applications and services that access data in Facebook.

API:

In computer programming, an application programming interface (API) is a set of routines, protocols, and tools for building software applications. An API expresses a software component in terms of its operations, inputs, outputs, and underlying types. An API defines functionalities that are independent of their respective implementations, which allows definitions and implementations to vary without compromising each other. A good API makes it easier to develop a program by providing all the building blocks. A programmer then puts the blocks together.

Setup:

First off all create a new project of Windows Forms Application type. Now we will install Facebook SDK.The easiest way to install Facebook SDK for .NET is using Nuget. Just search for Facebook on nuget.org OR Click Here (Facebook on nuget) .

facebook sdk on nuget

After selecting SDK link we will redirect to page as shown below

facebook nuget command

Now we have install command for nuget package, our next step is to install this package in our project.

To install nuget package select tools and follow steps as shown in picture.

After console window appear type command
Install-Package Facebook -Pre as shown in picture

get nuget console

facebook nuget install command

Say Hello to Facebook SDK:

Retrieving data form the Facebook Graph API is very easy using the Facebook SDK for .NET. The following code shows how to call the Graph API to retrieve nWebFront public information.

var fb = new FacebookClient();
dynamic info = fb.Get("nWebFront");				
				

The result of this request is a dynamic object containing various properties such as id, about, user name, etc. You can see the values of this request by browsing to http://graph.facebook.com/nWebFront in your web browser. The JSON result is shown below.

{
   "id": "384449535016868",
   "about": "Find free and premium tutorials and softwares",
   "can_post": false,
   "category": "Internet/software",
   "checkins": 0,
   "cover": {
      "cover_id": 562039717257848,
      "offset_x": 0,
      "offset_y": 0,
      "source": "https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfa1/t31.0-8/s720x720/10633958_562039717257848_335476115668619160_o.png",
      "id": "562039717257848"
   },
   "has_added_app": false,
   "is_community_page": false,
   "is_published": true,
   "likes": 946,
   "link": "https://www.facebook.com/nWebFront",
   "name": "nWebFront",
   "parking": {
      "lot": 0,
      "street": 0,
      "valet": 0
   },
   "talking_about_count": 115,
   "username": "nWebFront",
   "website": "http://nWebFront.com",
   "were_here_count": 0
}				
				

Now info dynamic object contains all properties as json object shown above. we can access it as

string id = info.id;
string about = info.about;
string website = info.website;
				

We can access only public data by using above code. Most API calls must be signed with an access token (An access token is an opaque string that identifies a user, app, or page and can be used by the app to make graph API calls). To overcome this problem we have to create Facebook App.

Creating a Facebook Application:

We must create a Facebook application. To do so navigate to Facebook Apps and click the Create New App button. facebook create new application

After click on Create New App button we will see another window as shown in picture. Now click on Advanced Setup as shown below

add new app facebook

Now fill out form as you want. Add Display Name (This will display when other users use our application), next add namespace if you want. Select any category and press Create App ID button as shown below.

create new app facebook

Accessing Private Information:

Now that you have seen how to make a request of public information using the Facebook SDK for .NET you probably want to do something a little more interesting. In order to access any information that is not public such as a user's profile details, friends list, or Time Line posts you need to provide a valid access token when making the request.

Getting Access Token:

For the purposes of this walk through we are going to start by obtaining an access token from Facebook's Graph API tool. You can find that tool at Get Access Token. as shown below

access facebook token

Now that you have obtained a valid access token you are ready to make a request for private data by following code.

string _token="access_token goes here";
var client = new Facebook.FacebookClient(_token);
dynamic me = client.Get("me");
string firstname = me.first_name;
				

Note ("The user tokens listed here are provided for convenience to test your apps. They expire like any other user access token and should not be hard coded into your apps.")

So if your token expire and you want to test more then regenrate from here Access Token

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected