Steve Trefethen
Contact me
About Me View my LinkedIn profile

Powered by discountASP.NET
referal ID: sdtref
Why recommend discountASP.NET?
Need consulting?
Need Consulting?

Disclaimer

The posts on this weblog are provided AS IS with no warranties, and confer no rights. The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

An HttpHandler for use with Facebook

June 18 2009 5:40AM

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:

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

The HttpHandler is an abstract class with a single abstract method called FacebookRequest descendents override to handle the request. As you can see below the handler is quite simple, parsing the queryparams into a Dictionary, creates an instance of the FacebookService and calls 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.
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags: , ,

Comments

6/20/2009 2:55:14 AM #

Surjanto

Very Nice !!!

Surjanto

6/22/2009 12:40:48 PM #

Steve Trefethen

Thanks!

Steve Trefethen

7/27/2009 9:30:23 PM #

Ilan

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

Ilan

2/2/2010 9:40:51 AM #

jim

Taking a look and the app seems broken. Popup error msg with following text:

We cannot locate the data file
  www.stevetrefethen.com/SocialMine/friendsjson.aspx
Check that the file name is correct.

jim United States

2/2/2010 5:21:27 PM #

Steve Trefethen

Hi Jim,
  That error has since been resolved. Had to update my SQL Server password.

Steve Trefethen United States

3/4/2010 2:34:30 PM #

Nomi


Helped me alot, Owe you


Btw, Where is Pakistan in Country List? I am not from Egypt

Nomi Egypt

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Spam filtering provided by: Spam Counter
336 comments approved, 1481 spam caught since October 28, 2009
Powered by Commentor