Monday, October 1, 2012

Getting Facebook data using FQL


After we managed to log into facebook in my previous post now it's time to get some data from facebook.
Facebook allows us to query its data using 3 ways:
1.       Legacy REST API – allows you to send requests and receive data via HTTP requests. Facebook are now in the process of deprecating this feature.
2.       Graph API
3.       FQL – Facebook Query Language
All facebook data is stored in huge tables (e.g. album, user, photo...), both Graph API and FQL allow you to query that data. Graph API makes you send http requests with the suitable parameters to get what you want as oppose to FQL that allows you to write some sort of SQL kind of language to query the facebook data.

Graph API example

Try them!  You'll receive a JSON response (which by the way you can switch to XML if you change the header of your request) with all your friends and all your albums details.


FQL

In my opinion it is a better way to query data from facebook because it reminds me SQL that is simple and obvious. It allows me to filter my queries adding a "where clause" that is used on indexed fields to increase performance and it "looks" better (readable and clear) when using it from .net class.

Let's see how we can retrieve personal details from facebook to our website

The first thing you need to do is go to http://developers.facebook.com/docs/reference/fql/ and scroll to the bottom where you'll see all the tables that can be queried. The tables have pretty obvious names (I admit that I still haven't understood why particular tables exist but I'm working on it) so as their columns. You can see the columns and their description in every table (e.g. user table - http://developers.facebook.com/docs/reference/fql/user/ ).
 Similar to SQL, FQL allows you to retrieve only the data that you want using the SELECT statement.  Opposed to SQL, FQL requires you to use the WHERE clause not only in every query you run but you can query only fields that are indexed (makes sense…you don't want to wait for a query that gets all the facebook users).

We want to receive some basic details about the user that is logged in our website. Mmm let's say we want his/her first name, last name, gender, profile picture and friend count. That means we need the following fields (according to the user table): first_name,  last_name, sex, pic_square and friend_count.
FQL is built like a basic SQL query: SELECT field1, field2 FROM tableName WHERE
Our query is: SELECT first_name,  last_name, sex, pic_square, friend_count FROM user WHERE uid=me()
Me() is a special facebook function that retrieves the logged in user id.

Now let's run it from our website!

First we create a new controller "UserDetailsController"






In order to present the user details in the View we'll need a model: "FacebookUserModel". The query retrieves JSON result so in order to desirialize the JSON to an object we'll have to create an object with the exact identical fields as the JSON response. One more thing: even though the query will retrieve just a single user the response is built like the query is supposed to retrieve an array of users so that our model will hold a list of "FacebookUser" which will hold our 4 properties (no .net standards =\)











Alright now we have our model, let's add some logic to the controller. First we need to connect to the facebook servers using the access token we received when we logged in, then we need to send our FQL and receive the JSON response. To receive a response from facebook we'll use the "Get" method that takes 2 arguments: path and object. Path tells facebook that is the second argument type – in our case it's "fql". The second argument will hold the query itself.
Facebook return JSON result that the best way to "hold it" is using dynamic object. If you'll open your debugger you will see that response starts with the word "data" and the whole response is surrounded with "[ ]" brackets" because the response is an array.
After we hold our JSON response all we have to do is desirialize it to an actual object (To use the JsonConvert nugget "json" and add it to your project – it's helpful). Remember! We don't  deserialze the response to the "FacebookUserModel" but to a list of "FacebookUser".

The last thing we have to do is send the "FacebookUser" object to our view. Oh right, we need to create a view…Right click inside our method and choose "AddView". In the settings window choose "Create a strongly-typed view" and choose the "FacebookUser" model class.


Doing so we enable our view to access the model properties that the controller sent it.

Simple html table with all the user's fields:


F5 the solution and login. Now add to your address the controller name.


 


Cool right?

2 comments: