Monthly Archive for May, 2008

De-mystifying Flash/Flex Facebook Applications Part 1

I wanted to write this post hopefully to help anyone develop flash applications that are running within Facebook’s canvas. There are currently 3 options in the AS3 Facebook API to hook up to facebook, namely startDesktopSession, startJSBridgeSession and startWidgetSession. Let me briefly talk about these 3 different ways of connecting and which one you should use to work with the facebook API.

startDesktopSession()

This method of connecting to the Facebook AS3 API is mainly for flash/flex applications running in webpages that have no relation with the facebook canvas. This applies to AIR applications as well.

When you use startDesktopSession, you generally have to supply your API_KEY and SECRET_KEY and navigate the user to facebook’s login page.

After that is done, you have to validate the desktop session after which you will receive an session_key that is limited or infinite depending on whether the user chose to save the login information.

Thereafter the API is ready for calls.

startJSBridgeSession()

This method of connecting to Facebook makes use of the facebook javascript API to make the calls and proxy the results back into the SWF using externalInterface calls.

You can see the javascript API on facebook at http://wiki.developers.facebook.com/index.php/JavaScript_Client_Library which has a pretty good example on hooking up the javascript API. (not a SWF example though)

The startJSBridgeSession simply makes similar calls using ExternalInterface from within flash and then gets the results back through a javascript callback.

Personally i haven’t had found any use for this yet.

startWidgetSession()

Use this if you are trying to make API calls from a flash app that is nested within an fb:iframe or fb:swf. You basically have to pass in your stage.loaderInfo.parameters which contains all the information the API requires to create a signature so that it can make proper API calls to facebook.

The API says this is depreciated but until there is a better way of calling APIs within flash (like facebook taking away allowScriptAccess, though highly unlikely), I would recommend this way of calling the API.

Important Note when using the 3 different methods

For those who are lazy to peek into the code within the API, for startDesktopSession(), asynchronous calls are made so it doesn’t matter if you add an eventListener before or after the startDesktopSession() to capture the results from facebook. However, for startWidgetSession() and startJSBridgeSession(), they are synchronous calls (meaning code executes line after line and the next line will not execute until the previous has finished executing) and you must add your eventListener before those functions or else the event handler will not fire. To be safe, add all your eventListeners before the call to start a session.

e.g.

var fb:Facebook = new Facebook();
fb.addEventListener(”complete”, fbCallback);
fb.startDesktopSession( ….. ) //can be startWidgetSession() or startJSBridgeSession()

Extra Misc Notes

If you are using custom headers with URLRequest for crossdomain calls, remember that for your crossdomain.xml, you must explicitly allow custom http headers within the crossdomain.xml. Adobe has a very comprehensive white paper for Flash Player security and its really worth a read.

You can find it at: http://www.adobe.com/devnet/flashplayer/articles/flash_player_9_security.pdf

An example of a crossdomain.xml that allows custom headers:

<?xml version="1.0"?>
<cross-domain-policy>

<!– Establish a meta-policy –>
<site-control permitted-cross-domain-policies=”master-only”/>

<allow-http-request-headers-from domain=”*” headers=”*”/>

<!– Data loading permissions are separate, and don’t affect headers –>
<allow-access-from domain=”*” />

</cross-domain-policy>

*Important for Flash CS3 users* If you are using the latest release from the google code SVN as of May 23rd 2008, search through the AS files in the pbking package and comment out import mx.controls.Alert, that code is meant for flex users.

Hope that this article will help anyone developing facebook applications with Flash/Flex/AIR.

For examples and code, there is a facebook authentication article on google code that gives the specifics of these 3 modes of authentication:

http://code.google.com/p/facebook-actionscript-api/wiki/FacebookAuthentication

I will post my own examples in the next few days, so watch this space ;)

Cheers!

Support PhotoAid for the Myanmar victims

This post is totally unrelated to RIA and stuff but i figured that it was important to be aware of happenings around the world and for developers like us to do our part to help these people (:D)

If you happen to be in Singapore or are in Singapore, head down to the merlion park opposite the Fullerton hotel to take a photo by the photoaid photographers against the merlion and the proceeds will all go to the Salvation Army who will then relay the funds to the Myanmar victims.

*I had just realised after a long time that the comments functionality in my blog was broken, will fix it and change this theme away

Update: My comments are working! I’d love to hear from my visitors! (I have a sudden nostalgia for the web 1.0 guestbook :D )

FUG Facebook Presentation and Flash Player 10 availability on Adobe Labs!

The presentation yesterday was wonderful, thanks to FUG for the invitation to speak on facebook applications. It was great to catch up with some of the guys in the UG especially Ryan whom I have not met for quite some time. Ryan runs flashmove.com and they have a pretty awesome community there in the forums.

My slides and a recording of the presentation are available on fug.sg and the Memory Leak/GC demo is available at http://alvinzhang.com/gctest/index.html

I have made the source available on that page as well. Also took some time to file the online bug report to Adobe, hopefully they can resolve it asap, especially when we have first glimpses of flash player 10.

Flash Player 10
The new flash player 10 is off the hook. Im very impressed with its new features like the support for hydra filters (custom filters) and 3D support coupled with hardware support for rendering graphics, so for guys who have that ever powerful Geforce 9800 in your system, it can finally work its engines on this new version of flash player.

Unfortunately, some quick tests reveals that the new flash player 10 suffers from the same memory leak that flash player 9 suffers from (that is demonstrated in the GC demo above, you can test it against FP10 as well). Hopefully the guys over at Adobe fix this ASAP in time for the release of the flash player 10.

I have not tested the connection bug where downloads persist after the unload of a loader object. Will do it when I have the time.

Lastly those cool demos on http://labs.adobe.com/technologies/flashplayer10/demos are crippled by the inability to download the sources :( Please fix this please!

You can get Flash player 10 at Adobe labs: http://labs.adobe.com/technologies/flashplayer10/

Exciting times lie ahead for the RIA scene.