(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Facebook Graph API and Silverlight

(8 votes)
Lazar Nikolov
Lazar Nikolov
Joined Dec 15, 2010
Articles:   7
Comments:   59
More Articles
5 comments   /   posted on Oct 19, 2011
Tags:   lazar-nikolov , facebook
Categories:   Data Access

This article is compatible with the latest version of Silverlight.


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?


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:


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 );
this.facebookClient.DownloadStringAsync(new Uri("https://graph.facebook.com/" + Profile + "/events?access_token=" + AccessToken));

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:

this.facebookClient.DownloadStringAsync(new Uri("https://graph.facebook.com/" + Profile + "/friends?access_token=" + AccessToken));

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.


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!



  • MaximeLamure

    Re: Facebook Graph API and Silverlight

    posted by MaximeLamure on Oct 20, 2011 13:06

    Thanks for your post. But could you explain us how to add a FB like button in a SL page ? (without using windowless of course).


  • lnikolov

    Re: Facebook Graph API and Silverlight

    posted by lnikolov on Oct 21, 2011 02:46

    Hm...nice question. According to the Graph API docs you can like any object that has a /likes connection. And, again, the page has /likes connection, so you might be able to post a like...However, I have not faced this so far. Having in mind all this, you should be able to make a button, style it as FB like button and like a page in the code behind. If docs say right. But, eventually, you will have to deal with authentication forms...tough stuff..

    The easiest way to do this is to use the social plugin - http://developers.facebook.com/docs/reference/plugins/like/. Right, you must use the generated html and place it in the host page, but, in my opinion, this is the best you can do. I used the like button social plugin several months ago and it worked just fine!

    Your last option may be to use some of the FB API wrappers from codeplex. 

    This is all I can think about. If anyone has a better solution, please share it here.

  • ReinhardZwisler

    Re: Facebook Graph API and Silverlight

    posted by ReinhardZwisler on Nov 25, 2011 00:20
    it dos not work for me
       "error": {
          "message": "Error validating application.",
          "type": "OAuthException"

    { "error": { "message": "Error validating access token: Session has expired at unix time 1318374000. The current unix time is 1322173147.", "type": "OAuthException" } }
  • Singles55

    Re: Facebook Graph API and Silverlight

    posted by Singles55 on Feb 01, 2012 06:15
    This was really helpful.
  • -_-

    Re: Facebook Graph API and Silverlight

    posted by on May 11, 2013 09:44
    thanks for sharing Bridesmaid Dresses

Add Comment

Login to comment:
  *      *       

From this series