An HttpHandler for use with Facebook

imageIn my SocialMine Facebook application, which uses MIT’s Simile Exhibit, I needed to fetch Facebook FQL query results from the Exhibit client-side JavaScript. While it may not sound that complicated it’s not something directly supported by the Facebook Developer Toolkit (FDT). I created an HttpHandler to respond to the Exhibit requests for the JSON data which populates the Exhibit. The HTTP request includes a Referrer header that contains the necessary data, fb_sig_user and fb_sig_session_key to construct an instance of facebook.Components.FacebookService. For completeness here is a list of the queryparams contained in the Referrer URL with the important lines highlighted:


My HttpHandler is an abstract class with a single abstract method called FacebookRequest that descendents override to handle the request. As you can see below the handler itself is quite simple, parsing the queryparams into a Dictionary, creating an instance of the FacebookService and calling FacebookRequest passing the service instance and the HttpContext passed to the handler.

public abstract class FacebookHandler : IHttpHandler
    private const string REQUEST_SESSION_KEY = "fb_sig_session_key";
    private const string REQUEST_USER_ID = "fb_sig_user";
    #region IHttpHandler Members
    public bool IsReusable
        get { return false; }
    void IHttpHandler.ProcessRequest(HttpContext ctxt)
        facebook.Components.FacebookService f = new facebook.Components.FacebookService();
        f.ApplicationKey = WebConfigurationManager.AppSettings["APIKey"];
        f.Secret = WebConfigurationManager.AppSettings["Secret"];
        Dictionary<string, string> dic = new Dictionary<string, string>();
        string[] split = ctxt.Request.UrlReferrer.Query.Split(new char[] { '?', '&', '=' });
        // Skip the first item since it’s the "?"
        for (int i = 1; i < split.Length - 1; i += 2)
            if(i < split.Length)
                dic.Add(split[i], split[i + 1]);
        f.SessionKey = dic[REQUEST_SESSION_KEY];
        f.uid = Convert.ToInt64(dic[REQUEST_USER_ID]);
        FacebookRequest(f, ctxt);
    protected abstract void FacebookRequest(facebook.Components.FacebookService fbservice, HttpContext ctxt);

The JavaScript of Simile Exhibit requests the JSON data using the following LINK tag:

<link href="friendsjson.aspx" type="application/json" rel="exhibit/data" />    

The reference to friendsjson.aspx isn’t actually an ASPX as it’s handled by a descendent of this HttpHandler which is registered in the httpHandlers section of the web.config as follows:

<add verb="*" path="friendsjson.aspx" type="FBFQL.JSONHandler" />

In my application the FacebookRequest method queries using FQL for profile data and returns a JSON result that populates the Exhibit.

Btw, if you’re looking for help on Facebook development be sure to read my wiki article.

6 thoughts on “An HttpHandler for use with Facebook

  1. Hi Steven,
    First of all, thanks for the starter kit, it’s amazing.
    I’m stucked when deploying my application on the server (from my host it works – I have a public IP)
    In production, I get an exception when accessing the API: Master.API….
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    Can you point me in the direction?

  2. Helped me alot, Owe you
    Btw, Where is Pakistan in Country List? I am not from Egypt

Comments are closed.