CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • 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
Home Development Beginning With Facebook Graph API C#.NET

Beginning With Facebook Graph API C#.NET

Rana Ahsan January 16, 2011 21 Comments


 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 🙂

Related

Filed Under: Development Tagged With: c#, facebook

About Rana Ahsan

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

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.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
  • Facebook C# API Tutorials
  • LinQ To SQL Database Update Operations In C#
  • Using Supervisord Web Interface And Plugin
  • Tutorial On Uploading File With CodeIgniter Framework / PHP
  • Utilizing Config File In C#.NET Application
  • Using GIT Plugin For Netbeans IDE

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • S. Chalisque on PHP HTML5 Video Streaming Tutorial
  • Armorik on Generate HTTP Requests using c#
  • iswaps on PHP HTML5 Video Streaming Tutorial
  • TAKONDWA on PHP HTML5 Video Streaming Tutorial
  • rorenzo on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2022 · CodeSamplez.com ·

Copyright © 2022 · Streamline Pro Theme on Genesis Framework · WordPress · Log in