Why does Facebook Actionscript API init() not work?

cammil picture cammil · Dec 9, 2010 · Viewed 8.4k times · Source

I am calling com.facebook.graph.Facebook.init(APP_ID, loginHandler);

to log in to facebook using the actionscript api (as explained in this tutorial: http://www.adobe.com/devnet/facebook/articles/flex_fbgraph_pt4.html). But, it does not seem to do anything.

Looking at the API source, the only call that is made to JS is ExternalInterface.call('FBAS.init', JSON.encode(options));

The two javascript libraries which are required are:

http://connect.facebook.net/en_US/all.js
FBJSBridge.js

However, i cannot find any reference to FBAS in these libraries. Am I even using the correct libraries? Is there another reason I have missed as to why FBAS doesnt exist? or even why calling Facebook.init() does nothing?

Answer

Aaron Horne picture Aaron Horne · Jan 14, 2011

FBJSBridge.js has been embedded in the SDK. Look at com.facebook.graph.core.FacebookJSBridge.as, and you'll see that the entire block of javascript that was FBJSBridge.js is tacked onto the ExternalInterface in com.facebook.graph.Facebook.as 's constructor.

I have been struggling with this all day. I have been watching the http GET spawned by Facebook.init(), and it looks like it just waits until it times out, at which point another GET is spawned. It never comes back normally. I tried the APPID, the APIKEY in there and all sort of combinations of parameters, but I never saw it come back. I have to conclude that's how its meant to work. If not, then that's how it does work. :-/

Anyway, you'll see inside FBAS in that JS block in FacebookJSBridge.as that init subscribes to the "auth.sessionChange" event which triggers Facebook.handleSessionChange(). You can see that is where your callback is called. The trick is that that is what gets called anytime the sessionChanges. So even if the next time that event fires isn't because of a response from FB.init(), your callback will still get called at that point.

If you set your own handler for "auth.sessionChange" before you call Facebook.init(), and you set "cookie" to true then after a cookie has been created you'll get a sessionChange event firing right after you call init. Init's callback still doesn't get called though because the GET never gets a RESPONSE.

Facebook.addJSEventListener("auth.sessionChange", onSessionChange);
Facebook.init(_appID, onInitFacebookConnect, 
      {cookie: true, 
       appId: _appID, 
       perms: "user_birthday,read_stream,publish_stream"});

The examples that you'll find in that tutorial may work, but are not what you'll want to use when actually putting an app together. Otherwise you are always going to click a button to trigger fbconnect. It looks like oauth2 will be the way to get the desired behavior of not needing to click on a button each time to "connect". You can get the sessionkey from oauth2 and then use the Flash/Facebook GRAPHSDK or JS depending on whichever is most convenient for what you are trying to do.

This might help: http://blog.yoz.sk/2010/05/facebook-graph-api-and-oauth-2-and-flash/