This article is compatible with the latest version of Silverlight.
Introduction
Facebook, Facebook, Facebook. It’s a constant part of our daily life. Contacting your friends thousand miles away (where your day is their night), spending your free time on the net, bragging for your new acquisition or where you’ve been, Facebook is all around.
And not only in you, it’s also in the applications you use. An amazing amount of web sites integrate with Facebook and, I believe, more and more applications are to use the social network to access data.
Recently, it was my turn to import Facebook data into my application. To share my experience with you and save you several hours of research, in this article I intend to face you with the whole world of something very cool – Facebook Graph API. Moreover, I will show you the alternatives and lead you through the flow when you access Facebook data.
Why the Graph API?
http://developers.facebook.com/docs/reference/api/.
You can also use some Facebook API wrappers that you can find on codeplex. But I prefer the Graph API. At least I have experience with it and not with the alternatives. You are always free to play depending on what you need.
Okay, nice. But how can I consume the data and how Facebook presents it to me? It’s simply JSON...Every connection of an object in the social network in JSON. And codeplex provides you JSON.NET - http://json.codeplex.com/. Anything else you need?
Unfortunately, yes. You do.
Setting an application
Almost every call to the Graph API requires you to have previously set a developer application in the portal. To do this you follow the steps on https://developers.facebook.com/apps.
After you finish, you get an application id and application secret. These both are used to generate an access token. This token is crucial. You can NOT do almost anything without the token. Moreover, the token is even more complicated than this.
So, when you have the id and the secret, you get the token by calling:
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET’
Just replace APP_ID with your application id and APP_SECRET with your application secret. If both are correct you will see the token.
But, you can use this token only for public data like the events of a company (page), etc.
To get a token that allows you to access more complicated data, you must use permissions. The easiest way to generate a token that allows you, for example, to access your events, friends and friend’s photos and so on, is to use the Graph API Explorer. Or, just go here - https://developers.facebook.com/tools/explorer.
See, you get a token for your specific needs in a second!
Accessing data
Here is an example. I’ve generated a token that allows me to access my events. Precisely, the events I’ve responded with “attending”.
To get my events, I need to know the id of my profile in the Graph API. Remember? My profile is an object! To make all easier, Facebook can handle you profile address (that is the username) instead of the id. My username is “lazar.nikolov” (public link for my profile – “facebook.com/lazar.nikolov”) – you have yours! Just check for it in you profile information.
So, the base part is “https://graph.facebook.com/lazar.nikolov”. To get the events I add “/events?”. Finally, I add the access token, preceded with “access_token=”.
And I get this:
{
"data": [
{
"name": "SANDER VAN DOORN - ELEVE11 Album World Tour 2011",
"start_time": "2011-11-05T05:00:00+0000",
"end_time": "2011-11-05T13:00:00+0000",
"location": "Arenele Romane (Carol Park)",
"id": "247241851961568",
"rsvp_status": "attending"
},
{
"name": "The Mission presents: Armin Van Buuren, Sunnery James & Ryan Marciano",
"start_time": "2011-10-16T05:00:00+0000",
"end_time": "2011-10-16T13:00:00+0000",
"location": "Sala polivalenta",
"id": "156220381126225",
"rsvp_status": "attending"
}
],
"paging": {
}
}
Yeah, I am a hopeless trance music fan...
Now it's easy!
To do this in Silverlight, you don’t need any special equipment. Something that one must know is that Silverlight uses asynchronous data retrieval model. Look at this:
WebClient facebookClient =
new
WebClient();
this
.facebookClient.DownloadStringCompleted +=
new
DownloadStringCompletedEventHandler( facebookClientDownloadStringCompleted );
The WebClient is the easiest way to do all the stuff. You must have your profile id/name and the access token and can retrieve all kind of information using the Graph API.
If you need your friends, generate the appropriate token and use:
Using the Newtonsoft.Json library from codeplex you can deserialize the JSON and iterate through your friends. Don’t forget, every friend is a profile, and thus an object in the social network. Being an object, it has id that you can retrieve and use to access additional information.
Conclusion
In this article I give you the basics when using the Facebook Graph API in your Silverlight application to make it social. But not all ends here. It is not only accessing information but also posting! The Graph API allows you to do practically everything you would need. Comment a status update, get profile pictures, publish on a wall, upload a photo...explore the entire social graph! And most important of all – enjoy!