Based on this awesome tutorial video posted here: http://www.adobe.com/devnet/facebook/articles/video_facebook_quick_start.html using Flex Builder and Actionscript, i was able to replicate the functionality using FlashDevelop and haXe.
Here is the process i followed to accomplish this:
- Download the Facebook API Actionscript library from here: http://code.google.com/p/facebook-actionscript-api/
- Open a command prompt and navigate to the folder where the file was downloaded
- rename the file from *.swc to *.zip
- Extract the archive with your archiving utility of choice.
- you will get two files, catalog.xml and library.swf
- the catalog.xml is not important to us, only the library.swf is.
- Make sure you have the haXe binaries on your path and execute this command:
- “haxe –gen-hx-classes library.swf”
- This will create a sub-directory called hxclasses that will contain the complete package structure of the library.swf comprised of haXe classes that are only stubs to the real classes inside the swf.
- Now fire up FlashDevelop and create a new AS3/Haxe project.
- Copy the contents of the hxclasses folder into the src folder of your new project.
- Add the library.swf file to your project, and then right-click it in the project explorer and select ‘add to library’. this will ensure the library.swf is referenced by the haxe compiler so the actual implementations of the classes can be included in the resultant bytecode.
- Now that our project is setup, we can start writing some code.
- In Main.hx, add the following code:
-
import com.facebook.commands.users.GetInfo;
-
import com.facebook.data.users.FacebookUser;
-
import com.facebook.data.users.GetInfoData;
-
import com.facebook.events.FacebookEvent;
-
import com.facebook.Facebook;
-
import com.facebook.net.FacebookCall;
-
import com.facebook.utils.FacebookSessionUtil;
-
import flash.display.Sprite;
-
import flash.events.MouseEvent;
-
import flash.Lib;
-
-
class Main {
-
static var facebookSession:FacebookSessionUtil;
-
static var mc:Sprite = new Sprite();
-
-
static function main() {
-
facebookSession = new FacebookSessionUtil("api key goes here", "secret key goes here", Lib.current.loaderInfo);
-
facebookSession.addEventListener(FacebookEvent.WAITING_FOR_LOGIN, handle_waitingForLogin);
-
facebookSession.addEventListener(FacebookEvent.CONNECT, handle_connect);
-
facebookSession.login();
-
}
-
-
private static function handle_waitingForLogin(fbe:FacebookEvent):Void {
-
mc = new Sprite();
-
mc.addEventListener(MouseEvent.CLICK, handle_close);
-
mc.graphics.beginFill(0xFF0000, 1.0);
-
mc.graphics.drawRect(0, 0, 100, 100);
-
mc.graphics.endFill();
-
Lib.current.addChild(mc);
-
}
-
-
private static function handle_close(me:MouseEvent):Void {
-
facebookSession.validateLogin();
-
Lib.current.removeChild(mc);
-
}
-
-
private static function handle_connect(fbe:FacebookEvent):Void {
-
var call:FacebookCall = facebookSession.facebook.post(new GetInfo([facebookSession.facebook.uid], [‘name’, ‘pic_square’]));
-
call.addEventListener(FacebookEvent.COMPLETE, handle_infoComplete);
-
}
-
-
private static function handle_infoComplete(fbe:FacebookEvent):Void {
-
var info:GetInfoData = cast(fbe.data, GetInfoData);
-
var user:FacebookUser = cast(info.userCollection.getItemAt(0), FacebookUser);
-
trace(user.name);
-
}
-
}
For details of what the code above does, i recommend viewing the video on the adobe site.