Building flash facebook apps the naive way has an annoying side effect. Every time you run the app locally through CS4/CS5, it will open a browser window. You may or may not have to log in. Then, typically you must go back to your app and tell it via some dialog that you’ve logged in and it’s ok to continue to login process.
If you’re doing development that requires multiple user accounts (such as cooperative gameplay), this gets even more tedious because now you have to log users in and out for basic testing. This is a significant time sink.
Fortunately, there’s a straight forward solution this. It’s possible to run multiple simultaneous user accounts that log in silently. In my solution, the NUM_LOCK key toggles between account 0 and account 1 but a launcher-menu could also be used to select accounts.
This silent-login solution is poorly documented by Adobe, but the AS3 Facebook API already saves a cookie each time a user authenticates themselves with facebook.
The function getStoredSession() in FacebookSessionUtil.as uses the SharedObject::getLocal AS3 API to write and read a cookie. It’s based off the api_key and supports one user only by its current implementation. These are called ‘shared local objects’ by Adobe (.sol files), but are very similar to web browser cookies.
To take advantage of the ‘silent login’ you must do a number of things:
- Detect the condition where the app is running independent of the canvas:
if( !loaderInfo.parameters.fb_sig_session_key )
- Detect when a cookie is present and skip login, calling verify directly instead:
else if( session.activeSession.session_key != null )
You must ensure the user has offline_access permissions for any of this to work. Beware that the cookies are saved whether or not offline_access permission is granted!
Offline_access essentially allows you to use the session_key, which is generated uniquely during each login, infinitely (like an inifinite cookie).
Finally, you must handle the case where the cookie exists, but login fails due to lack of permissions or some other problem.
This solution works great for a single account. But what if you need to quickly switch between 2 accounts? My solution was to add support for a pair of cookies instead of a single cookie. I simply override the FacebookSessionUtil class and implement a smarter cookie function. This could easily be extended to support an unlimited number of users, but for my testing, I only need 2 accounts.
The constructor allows you to specify using the alternate user. I use the NUM_LOCK keyboard state to determine which user to use. This could easily become an integer, or uid, for unlimited user accounts.
public class ElectrolabFacebookSessionUtil extends FacebookSessionUtil
protected var m_altUser:Boolean;
// when alternateUser is true, a different cookie will be used. Used for easily toggling between 2 accounts for testing.
public function ElectrolabFacebookSessionUtil(api_key:String, secret:String, loaderInfo:LoaderInfo, alternateUser:Boolean = false )
m_altUser = alternateUser;
super( api_key, secret, loaderInfo );
* get the stored session for the set api
protected override function getStoredSession() : SharedObject
var cookie:String = api_key + "_stored_session";
if( m_altUser == true )
cookie += "_0";
return SharedObject.getLocal( cookie );
Below is a screenshot of running 2 copies of the game simultaneously. I’m debugging one in CS5, and another is running independently through Chrome. In theory you could run multiple copies of CS5 and debug simultaneously.
Share on Facebook