Developing Facebook app on your local machine

by James Shaw 12. January 2009 18:23

Facebook used to let a developer specify the Callback URL to be localhost (e.g., http://localhost:56570/MyApp/) for an IFrame-canvas app such that as you develop your app, you can just test it on your local machine before pushing it out to your hoster.  That's no longer the case: you cannot specify a callback URL containing "localhost" anymore.  So how do you keep using localhost for debugging purposes during development?

Here's what I did:

1. You need to have another Facebook app, say "My App Dev", registered for development purpose (you'll need one anyways once you've released your app and need to develop incremental features). 

 

2. In this app have settings that parallel the production Facebook app except the "Post-Authorize Redirect URL".  Have the "Post-Authorize Redirect URL" point to a small HTML file (say "TestPage.htm") on your hoster that redirects back to localhost :).  Create TestPage.htm like the following:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Untitled Page</title>

</head>

<body>

<script type="text/javascript">

var newWindowLocation = "http://localhost:56570/ThePageIWantPostAuthorize.aspx" +

                                    window.location.search; // get the query string that FB tags on, including "?"

window.location = newWindowLocation; // redirect to localhost

</script>

</body>

</html> 

So your "Post-Authorize Redirect URL" would be http://apps.facebook.com/myapp_dev/TestPage.htm", for example.

 

3. Make sure in your code use the right Facebook APIKey and Secret for the right release, since now that we have 2 "Facebook apps" for the same app.  With .NET for example, you can have the following in web.config:

 

<add key="APIKey" value ="My App's key"/>

<add key="Secret" value ="My App's secret"/>

<
add key="APIKey.debug" value ="My App Dev's key"/>

<add key="Secret.debug" value ="My App Dev's secret"/>

and use compile-time macro to determine which set of values to use in your code:

#if(DEBUG)

var apiKey = WebConfigurationManager.AppSettings["APIKey.debug"];

var secret = WebConfigurationManager.AppSettings["Secret.debug"];

#else

var apiKey = WebConfigurationManager.AppSettings["APIKey"];

var secret = WebConfigurationManager.AppSettings["Secret"];

#endif

The same compile-time macro can help you pick the right set of other properties like db connection strings, since you'll probably want to use a local database instead of the one on your hoster during development. 

With the above trick, with just one additional HTML file (TestPage.htm) uploaded to your hoster and an additional Facebook app registration, you are back to your happy self debugging on your local machine :).

P.S. Obviously this trick won't work for FBML-based canvas.

Tags:

Tech

My First Blog!

by James Shaw 24. October 2007 12:05

Hi, everyone!

This is my first blog!  A lot of people have been blogging for a while, but I just started.  It's kind of ironic considering that I'm in hi-tech.  Why?  Well I'm not that big of a talker in real life; digitally I'm not that big of a writer either.  My emails have always been succinct and to the point; just ask my sister, who used to write these long emails to my family only to get short responses from me Smile.

So why start now?  What really changed my mind was seeing the benefit of blogging, on the receiving/listening end that is.  Often when I have a question about something (technical or not), I can find my answers in somebody's blog after Googling it.  I've also found a lot of useful content in those aggregator sites on the web (digg, delicious, etc) coming from people's blogs.  In a way blogging has become the under-pinning of free information in a society.  Think about it; a lot of info made available to us today (from periodicals, TV, etc) have been filtered or even deliberately distorted by a few individuals; only on the internet, through blogging and mediums like digg.com do we have access to all info, from all sides of opinions.

So after benefiting from other bloggers for a while, it's time for me to also blog and join fully this free intellectual exchange.  Who am I and what will I blog about?  Well I'm a Software Engineer working for Intuit (the maker of Quicken, QuickBooks and Turbo Tax); I've been with the company since after college (see my linkedin profile).  I love the ocean and live in the beautiful San Diego.  In my spare time I like to (you guessed it) read others' blogs, play volleyball and salsa dance (doesn't mean I'm good at it).  

I will blog a lot about techie stuff (latest technologies, gadgets, etc); you can probably already tell from my geeky writing style.  I'm also into real-estate, finance/economics, history and travelling.  Of course once in a while I throw in some fun stuff to make you laugh Laughing.

So keep in touch and look forward to hear from you.  Feel free to check out my website (soon to be merged with this site) to see what I look like, and if you already know me, let's keep in touch in Facebook.

 

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO