What is Facebook Graph API?:
Facebook Graph API is one that Facebook supports, but it’s the major/core API with which most of the functionality can be implemented that Facebook provides support for third-party applications. So, anybody interested in creating a Facebook application must have knowledge of graph API. Besides retrieving/reading data from Facebook, this also provides methods for witting data to a user’s Facebook profile/page etc, via this graph API. I will here discuss the Facebook graph API c# tutorial step by step with proper code examples where applicable.
Primary requisites for FB graph API:
So, now we are planning to get started using Facebook API in C#. Firstly, we will need to download Facebook c# SDK . Here, I will like to inform you that this SDK in not a must, as that can be done using a simple HTTP request in c#. But in the case of authorized API calls, this SDK will help a lot for sure. Moreover, it’s better to use the API all the time so that you don’t have to do much re-coding if the application needs enhancements in future. After downloading the SDK, simply compile/build the Facebook project and import the generated DLL to your C# application as a reference.
Access Public information:
Let’s play an example of accessing public data from Facebook API. For accessing public data, Facebook provides a very simple way, just make a get call with the unique id of the corresponding entity object, and you will get a JSON result string with the corresponding information. Suppose we are willing to access a Facebook page of codesamplez.com, then we will have to make a ‘get’ method request from our Facebook c# SDK with the URL “/106181569450743” , which in turn will call this URL: “https://graph.facebook.com/106181569450743”.(Just to mention, 106181569450743 is the unique id for our codesamplez fan page). You can also paste this URL to a browser window and hit enter. You will see a result as follows:
{
"id": "106181569450743",
"name": "Codesamplez",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs478.snc4/50552_106181569450743_960056_s.jpg",
"link": "http://www.facebook.com/pages/Codesamplez/106181569450743",
"category": "Website",
"website": "https://codesamplez.com/",
"founded": "2010",
"company_overview": "codesamplez.com is a dedicated blog site for discussions on programming and developments. This blog will contain rich and informative contents day by day to help programmers/developers(from geek to professionals). This site doesn't limit its discussions on a specific programming language domain, rather discussions on this site will include help topics from all languages whenever possible.",
"likes": 7
}
Code language: JSON / JSON with Comments (json)
This is a JSON response string. Facebook c# SDK provides an easy way to access data from this JSON object.
Here are C# code samples for accessing our Facebook page for codesamplez:
FacebookAPI fbApi = new FacebookAPI();
JSONObject codesamplezObject = fbApi.Get("/106181569450743");
String companyOverview = codesamplezObject.Dictionary["company_overview"].String;
// use companyOverview variable wherever you need to for showing.
Code language: JavaScript (javascript)
Access Private Authenticated information:
To access private information, first, we will have to get the access token, which is provided by Facebook and is unique for a user. I have previously discussed how to retrieve Facebook API access token using c#.
After receiving the access token, you will have to set its value on the FacebookApi class’s object. After then, we are ready to make calls for the private data. But however, we won’t get access to all kinds of data even after this, as some data requires special extended permissions. If you don’t have a details idea, consider reading my other article for the Facebook authentication guide. After completing the authentication part, we are ready to query the FB API for accessing private data. Here is a sample code for getting a few private data about a user:
JSONObject tempMe = myFBAPI.Get("/me");
//name of the user who is accessing the app
string name = tempMe.Dictionary["name"].String;
//access homtown data requires special permission on 'user_hometown' scope
JSONObject hometown = tempMe.Dictionary["hometown"];
string hometown = hometown.Dictionary["name"].String;
Code language: JavaScript (javascript)
Publish Data:
Yes, we can publish data via Facebook graph API also. For this, you will need to have ‘publish_stream’ special extended permission. Now you can publish data by simply calling the ‘post’ method of the Facebook API library, passing 2 proper parameters:
- The API URL path where to publish data. Such as for publishing data to the user’s timeline/updating user status, use ‘/me/feed’ as this parameter.
- Dictionary data, to be published. Such as, if you want to update a status message, then simply use key-value pair like ‘message’=>’Message to be updated for the Dictionary object.
Here is a simple c# code sample for writing/publishing data(posting a link to user status) via Facebook graph API:
Dictionary<string,string> data = new Dictionary<string,string>();
data.Add("link","https://codesamplez.com");
myFBAPI.Post("/me/feed",data);
Code language: JavaScript (javascript)
References:
For introductory knowledge, official documentation on Facebook graph API is a very must-read for beginners. For who have already started using Facebook API should visit the graph API reference page for details on each method and their corresponding parameters, return value etc. I will try to post some more helpful articles from time to time. Happy coding 🙂
Diaa Fayed says
please, How I can get comments on a post for
some user in disktop application
/facebook-graph-api-c-sharp
Rana says
You will get comments from the status message object’s connection. Here is the facebook api documentation link: http://developers.facebook.com/docs/reference/api/status/
Mani Kandan A says
how to post message in facebook wall using facebook userid with out login.
Phil says
Hi Rana,
This drives me mad. I always get 403 forbidden error when i am trying to post stuff on the wall. I did pass “scope=publish_stream” as a part of query string for getting the access token.
Ant assistance from you will b highly appreciated.
Warm Regards
Phil
Phil says
I sorted it out. Thanks
Wayne says
Hi Phil,
I also get the same error. May I know how did you fix it?
Rergards,
Wayne.
jnana says
Hay wayne, even i got the same error .. i could able to fix this..
try like… to post your own data
Dictionary data = new Dictionary();
data.Add(“message”,”always use google to search”);
JSONObject postResult = myFBAPI.Post(“/me/feed”, data);
mostafa says
how can i get the fan page id programatically from my application that is hosted in facebook and added to a fan page as a tab. the application is hosted as iframe in my facebook application. i want to get the fan page id so i can query some information using facebook sdk.
appreciate your help.
Vishal says
How can I post comment on application wall from my website using c#.
And Phil how you solve this error ” 403 forbidden error”
appreciate your help.
EMAN says
I WANT TO GET A COMMENTS FROM POST FOR ANALYSIS USE SO FROM CERTAIN PAGE OR GROUP ,,,, HOW CAN I GET THE COMMENTS BY GET METHOD !!!????
PLEASE ANSWER ME AS SOON AS POSSIBLE
slimKhan says
i’m trying to do this exemple but i have a problem !
it told me that it need System.Web & System.Web.Extensions but i can’t find them when a go to add reference !!
Graphs says
These article provides some of the most valuable info. pertaining to codes. I appreciate the work.
Mandeep says
Thanks, its realy very helpfull.
Narendra Jarad says
nice one dear. thanks a lot for sharing.
msk says
How read all the comment from a FB page? I have created one fb page and make comment from another account. From application i tried to read all the comments. Am able to read only the comments which i posted from the same account that of page not the from other account.
Abhishek Kapoor says
How to Get Number of Likes From Facebook Page????
aishabhartibharti says
i want to post data from my website to facebook in c# windows application
Jochen Scheifele says
Hey, I built a c# Application with REST Api and can post a picture and message without problems.
But, when I post a second picture with a message, it will shown in the same Post on my wall like the first one and my message is hidden.
How can I post different photos in different Status-Posts on my wall?
Amar Sutar says
recently facebook change some authentication conditions because before some days i was trying to post on facebook using same code which is your using now but it wont work ,If you know better code to call api pls share