• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / Beginning With Facebook Graph API C#.NET

Beginning With Facebook Graph API C#.NET

January 16, 2011 by Rana Ahsan 21 Comments

Facebook C#

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": "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
}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","http://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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: c#, facebook

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Diaa Fayed says

    March 29, 2011 at 4:36 am

    please, How I can get comments on a post for
    some user in disktop application

    /facebook-graph-api-c-sharp

    Reply
    • Rana says

      April 3, 2011 at 3:33 am

      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/

      Reply
    • Mani Kandan A says

      May 22, 2014 at 7:35 am

      how to post message in facebook wall using facebook userid with out login.

      Reply
  2. Phil says

    April 3, 2011 at 8:45 pm

    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

    Reply
    • Phil says

      April 3, 2011 at 9:22 pm

      I sorted it out. Thanks

      Reply
      • Wayne says

        May 31, 2011 at 2:45 am

        Hi Phil,
        I also get the same error. May I know how did you fix it?

        Rergards,
        Wayne.

        Reply
        • jnana says

          August 5, 2011 at 2:38 am

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

          Reply
  3. mostafa says

    April 5, 2011 at 4:04 pm

    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.

    Reply
  4. Vishal says

    May 18, 2011 at 11:35 pm

    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.

    Reply
  5. EMAN says

    May 22, 2011 at 2:31 pm

    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

    Reply
  6. slimKhan says

    December 8, 2011 at 4:29 pm

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

    Reply
  7. Graphs says

    December 12, 2011 at 11:32 pm

    These article provides some of the most valuable info. pertaining to codes. I appreciate the work.

    Reply
  8. Mandeep says

    December 19, 2011 at 5:06 am

    Thanks, its realy very helpfull.

    Reply
  9. Narendra Jarad says

    June 26, 2013 at 1:49 am

    nice one dear. thanks a lot for sharing.

    Reply
  10. msk says

    December 29, 2013 at 7:20 am

    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.

    Reply
  11. Abhishek Kapoor says

    April 1, 2015 at 9:28 pm

    How to Get Number of Likes From Facebook Page????

    Reply
  12. aishabhartibharti says

    October 9, 2015 at 1:22 am

    i want to post data from my website to facebook in c# windows application

    Reply
  13. Jochen Scheifele says

    June 27, 2017 at 3:18 am

    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?

    Reply
  14. Amar Sutar says

    November 6, 2017 at 7:01 am

    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

    Reply

Trackbacks

  1. FQL Tutorial In C#/FacebookSDK | codesamplez.com says:
    April 28, 2011 at 9:19 pm

    […] you are a beginner Facebook api developer in c#, you should better start by another article about getting started with Facebook graph api in c#. Also, you should be familiar with facebooksdk library also to use the examples here. If not yes, […]

    Reply
  2. C# Tutorial For Retrieving Facebook Api Access Token | codesamplez.com says:
    May 9, 2011 at 4:05 am

    […] you have any questions. Also, to know more about facebook c#, please read my another article about getting started with facebook graph api in c# . Happy coding Share/Bookmark Related Tutorials:Using FQL With Facebook C# […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023