Facebook AS3 API Blues

While working on adding Facebook connectivity for a Flash based project, I found myself spending way too much time fighting with the Facebook-as3 API. For a Flash developer, there are two basic ways of using the Facebook API: directly with HTTP requests or with the help of Adobe’s facebook-as3 API. This library is maintained by Adobe, and you would expect it to be well documented and reliable, but it has some major issues, most of them are with the OAuth 2.0 authentication process.

How to make Facebook-AS3 API to actually work without going insane

Note that the last major change in the API was in April 2010 with the introduction of OAuth 2.0 in Facebook. At the time of this writing, version 1.6.1 is the current version of the API.

First, the setup for a web based project is a bit messy, since you need to register your Facebook app with a specific URL and put some code in the Html wrapper – for a full explanation read this post.

OAuth in AS3

Logging in a Facebook user with OAuth2 is simple enough if you use HTTP and JS. Issue a call with the AppID and permissions and it receive the access token in the JS callback function. Not so with the facebook-as3 API: you need to call Facebook.init() and then depending on the result call Facebook.login() to handle the login prompt.

Problem #1 - Facebook.init() returns (null, null)

The official documentation says Facebook.init will return a session on success and null on fail – however it actually returns only the session in the result, or null if you are not logged in. The second argument (fail object) is always null. Just this simple discovery had cost me a few days of hair pulling, so don’t rely on the docs. Yes, they are supposed to be updated frequently by Adobe but somehow this detail slipped out [facepalm].

Problem #2 –  Facebook.login() returns null regardless of actual login

Calling login right after Facebook.init() returns may not work as expected, and worse – results will vary depending on the browser and Flash player version (For me it works in Chrome and fails on IE8).

Don’t waste time on the official docs, since there is no mention of that there. It turns out that Flash player 10 imposes a security restriction (that may or may not be related to the browser) that will cause login to fail silently and return right away with null and lose connection with the login popup, which is very confusing since the API call to Facebook actually works in the browser, only not in your code [facepalm again].

My Solution

There is more than one solution to the authentication process, and it really depends on what you need. You could avoid the facebook-as3 library all together, as some developers on the forums suggested. There are no restrictions in HTTP/JS login, and it is painless. But what about the AS3 code?

After looking at a few samples, it appears that the simple solution would be to have a login button in your app, and require the user to click it to invoke the login popup. Simply put the Facebook.login call in the button handler function and it works perfectly.
However, you need to prevent subsequent calls to Facebook.login after a successful once, so make sure you hide or disable the button or face the consequences (I managed to crash IE8 with multiple login calls).  See sample code below:

	Facebook.init(APPID, onInit);
	...
 
	private function onInit(result:Object, fail:Object):void
	{
		btnLogin.visible = (result==null);
		if (result != null) {
			// We have a session
			trace("you are logged in");
		}
		else {
			trace("need to log in");
		}
	}
 
	// Login Button handler
	private function btnLogin_clickHandler(event:MouseEvent):void
	{
		Facebook.login(onLogin);
	}
 
	private function onLogin(result:Object, f:Object):void
	{
		if (result == null) trace("Access denied!!!");
		else {
			trace("Login complete.");
			btnLogin.enabled= false;
		}
	}

If anybody has a better solution I would love to hear.  Hopefully someone at Adobe will notice and modify the documentation to better reflect the reality, or even better – fix those issues inside the AS3 API so that it would be consistent with the HTTP/JS API.

,

3 Responses to Facebook AS3 API Blues

  1. Logos January 23, 2012 at 11:15 am #

    It behaves very strange in my application. It causes to close application (when I run it odd time) or does nothing (in even time) – alternate.

    I have no idea why is it so? Could you give me an advice? I am using 1.8.1 version API.

  2. Logos January 23, 2012 at 11:22 am #

    Ok, I am very sorry. I made a mistake.
    It runs always, but never goes to onInit.

  3. Danel June 28, 2012 at 3:45 am #

    onInit in my case is never called, im using MonsterDebugger to trace out which methods are actually called and NONE of the FB api suggested methods run. Only output i get is when i hit the actual login button. arghhh… every developer should have this slogan “Do it right and correctly or dont do it at all”. Hate buggy API’s