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:

auth_token=1379094a2a42e1c0037a33048912a875
fb_sig_in_iframe=1
fb_sig_locale=en_US
fb_sig_in_new_facebook=1
fb_sig_time=1245301045.1412
fb_sig_added=1
fb_sig_profile_update_time=1241328269
fb_sig_expires=0
fb_sig_user=719571200
fb_sig_session_key=c93185e145713ce98d72dfc2-719571700
fb_sig_ss=57abbef0284354d029fe8a19fa00dfce
fb_sig_ext_perms=offline_access,email,auto_publish_recent_activity
fb_sig_api_key=33c52d0a0c9867f677a1f05154c28478
fb_sig_app_id=5012360767
fb_sig=9ad664af23910ca18aeb203203c366cd

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);
    }
    #endregion
    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?
    ilan_levy@msn.com
    Thanks,
    Ilan

Comments are closed.