WCF + Facebook (break a RESTing fly on MS wheels) - Intro
A few weeks ago I was invited by a friend of mine to join Facebook. Fair enough, I tought, I had been looking for a nice excuse to join the system, since I had heard a number of things regarding the availability of platform functions through an API.
I first looked whether there was already a C#/.NET wrapper available in order to talk to Facebook, and found this one here. While the source code is readily available (a nice touch) I wasn’t too happy with what I saw (I am horribly picky when it comes to code, totally opposite to my usual “unfussedness”) - for once I don’t think one needs to rummage around in XmlDocument formed from the response and handpick the response parameters. Ever heard of deserialization? Anyway, don’t get me wrong, I looked a few times into the code later on - it enabled me to compare my own endeavours to working code, which is a real gain.
Furthermore, I was looking for a good excuse to learn something about WCF. There it was. Little did I know then that this would be the beginning of a 10-day tour de force into WCF (and incidentally, the facebook API).
WCF (Windows Communication Foundation) is .NET 3.0’s way of talking to remote resources. It follows the ABC paradigm that is also expressed in the Web Service Standards (Address, Binding, Contract). This intro to WCF Endpoints explains what is more or less meant.
Facebook opens up a few of its capabilities through an API. They call it RESTful - I call it weird. For once, the whole process of talking to facebook is not fully stateless. You will need to get a session. The way to get a session is fairly involved.
- Retrieve a token with a valid API key.
- Call a login page for the user using your application, passing along the obtained token and application key.
- Call a session creation with the token.
- Henceforth you will have to call facebook API methods passing along the obtained session key
- Not forgetting the added spice of security measure that an additional parameter must be sent that is all previous parameters hashed with a secret that was given to you in the beginning (and in fact is replaced when you call createSession)
Calling facebook is always via HTTP POST, with content type application/x-www-form-urlencoded. An exception is the upload picture method that has a content type of multipart/form-data. In other words, you’re submitting forms when talking to facebook. I have seen prettier things in life. For a full explanation of the facebook API, look here (Facebook API documentation). On a sidenote, if you want to see something that I would say is a lot closer to Representational State Transfer, check out the Twitter API documentation.
Would WCF be able to do something so…mundane?
With the keywords REST and WCF I found this link: Clemens Vasters - How to teach Indigo to do REST/POX. Alas, I found it a difficult read, which is why I didn’t finish it. A lot of text, a lot of code. You must excuse my current attention span, I am in desperate need of a holiday. Microsoft also had a go on the whole idea: Microsoft’s take on REST and POX in WCF. The one link I really read through and digested was the following: Juan Wajnermann - REST/POX client with WCF. Here he chooses to talk to del.icio.us with WCF, a web site I really adore. Best of all, he provides a solution to download. It worked right from the spot. Not too much code, some commenting, great! The solution therefore exposed to me numerous entry points to the WCF stack.
Also very useful for comprehending WCF are the WCF samples from MSDN for downloading and looking at, covering numerous aspects of WCF stack extension and customization. One of those extension points is using a Custom Text Encoder.
Why we need that will be the subject of the next post. (Damn I haven’t written it up still, motivate me!)