Beginning With Facebook Graph API C#.NET


 Beginning With Facebook Graph API C#.NET  
 


What is Facebook Graph API?:

Facebook graph API is one that Facebook supports, but its the major/core API with which most of the functionality can be implemented that Facebook provides supports for third-party applications.So, anybody interested to create a Facebook application , must should have knowledge in graph API. Besides retrieve/read 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 about 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 for 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 simple HTTP request in c#. But in case of authorized api calls, this sdk will help a lot for sure.Moreover, it’s better to use the API all the times so that you don’t have to do much recode 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 reference.

Access Public information:

Lets 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, you will get a JSON result string with the corresponding information. Suppose, we are willing to access Facebook page of codesamplez.com, then we will have to make an ‘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": "http://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
}

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.

Access Private Authenticated information:

For access private information, first we will have to get the access token, that is provided from 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 kind of data even after this as some data requires special extended permissions. If don’t have details idea, consider reading my another article for Facebook authentication guide. After completing the authentication part, we are ready to query the fb API for accessing private data. Here is sample code for getting 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;

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 Facebook API library passing 2 proper parameters:

  • The API URL path where to publish data. Such as for publish data to user’s time line/update 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 samples for writing/publish data(post a link to user status) via Facebook graph API:

Dictionary<string,string> data = new Dictionary<string,string>();
data.Add("link","http://codesamplez.com");
myFBAPI.Post("/me/feed",data);

References:

For introductory knowledge, official documentation on Facebook graph API is very good/a must read for beginners. For who, already started using Facebook API, should visit the graph API reference page for details on each methods and their corresponding parameters, return value etc. I will try to post some more helpful articles time to time. Happy coding :)

Comments

  1. 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

        • 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);

  2. 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.

  3. 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.

  4. 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

  5. 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 !!

  6. 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.

Trackbacks

Leave a Reply