Talk to Facebook Graph API VIA FQL,C#

This post is here to help you talk to Facebook’s Graph API through FQL and save the JSON results to a Database VIA C#.

FQL stands for Facebook Query Language,whose sematics are on similar lines of SQL but supports only a subset of it.

Before starting you should have few assembilies/permissions in HAND….


I know that there will be an Angry bird rounding your head Q’ing-‘Why do i need these Assemblies?’

Let me answer that by lying a Foundation stone.

  • As per FB’s Documentation

The Graph API presents a simple, consistent view of the Facebook social graph, uniformly representing objects in the graph (e.g., people, photos,events, and pages) and the connections between them (e.g., friend relationships, shared content, and photo tags).

Every object in the social graph has a unique ID. You can access the properties of an object by requesting

. For example, the official page for the Facebook Platform has id 19292868552, so you can fetch the object at

Full Read @

  • There are two ways you can talk to Graph API –Querystring and FQL

Here,I will be talking about the FQL version

Heads up About FQL @

Now,let me tell you why we need these Assemblies/permissions

  1. FB C# SDK is to talk to the Graph API VIA C# .
  2. Json.NET is to play with the JSON objects resulting from your API/FQL calls to Graph API.
  3. Graph API Explorer is the testing platform to check your FQL Queries for proper/perfect Data.


To make you understand better i will use an example of pulling Facebook Page Insights (Stats) from Graph API Via C# and FQL.

  • If you are familiar with Facebook pages like Coca-Cola or any other brands ,you should have surely heard about the term ‘Insights‘-They are the statistics about a page,which only an Admin can see statistically.An user can pull the stat details if and only if ADMIN provides you with an ACCESS TOKEN with appropriate permissions.

Facebook Page Insights-An Admin View


Creating a Facebook Application

Before you even begin using the Facebook C# SDK you must create a Facebook application. To do so navigate to and click the ‘Create New App’ button. Follow the steps to create an app and record your AppId for use later.


The easiest way to get started using the Facebook C# SDK is to install it using Nuget. If you don’t already have it installed, download it from If you do already have NuGet installed make sure you have the most recent version. So of the features used in the Facebook C# SDK will not work with old versions of NuGet.

Adding the Facebook NuGet Package

To add the package to your project simply right click on the references folder and select ‘Manage Nuget Packages…’. Search for the packaged title ‘Facebook’, select it, and then click ‘install’.

Configuring the Facebook C# SDK

After you install the package you must configure the application. The only setting we need to change is the ‘Site URL’ under the ‘Website’ settings. The Site URL must match the url you are using for local development. Set this URL to http://localhost:#### where #### is the port you are using for local development.

WARNING: There is a bug in the Facebook Javascript SDK that prevents you from performing logins when running on non-standard ports on localhost in Internet Explorer. You must either use port 80 or test this in a browser other than Internet Explorer.

Making Your First Request

Retrieving data form the Facebook Graph API is very easy using the Facebook C# SDK. The following code shows how to call the Graph API to retrieve MusicNLoud‘s public information.

The result of this request is a dynamic object containing various properties such as first_name, last_name, user name, etc. You can see the values of this request by browsing to in your web browser. The JSON result is shown below.

Accessing User Information

Now that you have seen how to make a request of public information using the Facebook C# SDK 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.

Obtaining an 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

Retrieving Profile Data

Now that you have obtained a valid access token you are ready to make a request for private data. Because this access token did not request any special permissions (discussed later) we will only be able to access limited details from the user.

  • You must have read_insights and read_stream permissions on the page before using the FQL Queries below.The access token should be generated with these permissions.


  • The above FQL query format is called FQL Multiquery,which will return JSON objects as Name-Value Pairs.
  • These queries will return insights data which we saw in the Image above.
  • To understand more about Insights table,Periods and other columns please go through   which will also give info about other tables and limitations.
  • Page_id and anchor_id (both same in this case) is the page id of the FB page whose insights you want to pull.
  • Run these queries in the FB GRAPH API explorer with proper table permissions and Access Token before using them in your code.


Polyglot & Pragmatic Programmer • Developer Advocate, IBM Cloud • Intel software Innovator • DZone MVB
(Visited 27 times, 1 visits today)

You may also like...

Show Buttons
Hide Buttons