![]() |
|||||||
Hybrid Hacking provides tutorials on how to integrate with some of today's hottest web services.
|
Building Facebook Applications with Flex
Sat, 06/30/2007 - 22:30
If you are looking to create a Flex application that connects to Facebook, the AS2 library won't help much since Flex is based on ActionScript 3. Luckily there has been some great work by a few ActionScript developers on Facebook. This tutorial demonstrates how to get up and running using Jason Crist's AS3 Facebook Library. The application you will build in this tutorial will simply list all of your Facebook albums inside a Flex component. This should be enough to get you started and give you a taste of how to connect to the Facebook API. Step 1: Add the Facebook Developer Application The first thing you need to do is add the Facebook Developer Application. Once you add this application to your Facebook profile you will be able to create new applications and access other developer resources. Step 2: Apply for a Facebook API Key After adding the Developer application, click on the "Create one" link to create a new application. Type in your application name and click the agree box if you accept the terms and conditions. Under the Optional Fields tab, you need to set the callback URL. This will normally point to a file on your web server, but for this simple demo we can put "http://example.com/". When you click submit you will see a new application listed:
Take note of the API key and secret key. You will need to put these strings into your Flex code shortly. 3. Download the Library Visit the Facebook ActionScript API Google Code Project, then click on Downloads and download the "facebook_as3_api_v0.1.zip" file. Thanks to Jason Crist of Terralever for providing this library to the development community. Create a new "FacebookTest" folder on your desktop and extract the "com" folder of the ZIP file there. 4. Create The Flex Application Start Flex Builder and create a new Flex project called "FacebookTest". Don't name your MXML file Facebook.mxml as it will conflict with classes in the library. Set the path to the "FacebookTest" folder you created on the desktop. Notice the com folder is in the project from when you extracted it earlier. Paste this code into your MXML file:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import com.terralever.facebook.Facebook;
import com.terralever.facebook.data.photos.FacebookAlbum;
import com.terralever.facebook.delegates.photos.GetAlbums_delegate;
[Bindable] private var facebook:Facebook;
[Bindable] private var photoAlbums:Array;
private var api_key:String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private var secret:String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private function init():void {
facebook = new Facebook();
facebook.addEventListener("ready", onFacebookReady);
facebook.startDesktopSession(api_key, secret);
}
private function onFacebookReady(event:Event):void {
showAlbumsButton.enabled = true;
}
private function getAlbumsReply(event:Event):void {
var delegate:GetAlbums_delegate = event.target as GetAlbums_delegate;
photoAlbums = delegate.albums;
}
private function getAlbums():void {
facebook.photos_getAlbums(facebook.user, getAlbumsReply);
}
]]>
</mx:Script>
<mx:Button x="10" y="10" label="Start using Facebook API" click="facebook.validateDesktopSession();" id="startButton" width="166"/>
<mx:DataGrid x="10" y="87" dataProvider="{photoAlbums}" width="166" height="170">
<mx:columns>
<mx:DataGridColumn headerText="Album Name" dataField="name"/>
</mx:columns>
</mx:DataGrid>
<mx:Button x="10" y="40" label="Show Albums" id="showAlbumsButton" click="getAlbums()" enabled="false" width="166"/>
</mx:Application>
Replace the API and secret key with the keys specific to your own Facebook application. 5. Testing
6. Conclusion Since an SWF can be decompiled and your secret key exposed, this is not the recommended way to build an application for public use. This example is meant for personal testing and to get an idea of the basics of creating a Flex-based Facebook application. Download the code for this tutorial - Facebook-Flex-Example-1.zip (30KB). Hopefully you found this tutorial to be a quick and easy way to get a Flex application connecting to the Facebook API. If you have any problems or questions please leave a comment below. Comments
Tue, 10/09/2007 - 14:08 — Mike (not verified)
Hey there, after following all your steps in Flex Builder 3. I paste the code and get this error: Severity and Description Path Resource Location Creation Time Id That line is: [Bindable] private var facebook:Facebook; Any clue what's going on?
Tue, 10/09/2007 - 14:57 — Mike (not verified)
Fixed that. This tut doesn't really work 100% with Flex Builder 3. The "com" folder needs to be inside the "src" folder generated by Flex. That's the tip I have there. Second question: I keep getting a Error 100 from Facebook. Even though my APIkey and secret are correct, it says missing/not supplied parameters. Essentially this means nothing works. The app brings up a browser window and I enter my user/password... but after that nothing works.
Thu, 10/18/2007 - 00:39 — Anonymous (not verified)
Question: How to load this application from facebook profile? Can you show the settings? Thanks.
Sun, 10/28/2007 - 02:22 — shaz (not verified)
Hi is there anywhere that this api of Jason Crist of Terralever has been explained for better understanding of how to use it or any tips on how to learn the api. thanks
Sat, 11/24/2007 - 08:03 — Kono (not verified)
Thanks, Jason Crist
Fri, 11/30/2007 - 03:19 — Jason Crist (not verified)
Thanks for the exposure! I just wanted to drop a note to say that I've made a lot of progress on the Facebook AS3 API since this was posted. I'm up to version 0.7 and there is a LOT more functionality. I've created a tutorial on how to use the API as well as the new features. Please don't hesitate to contact me if you have any questions or problems.
Tue, 12/04/2007 - 22:01 — Anonymous (not verified)
i have a question. i want to test this application on facebook but i cant seem to test it. how does one test their own application. its driving me nuts :(
Wed, 02/27/2008 - 00:14 — Alex Notov (not verified)
Excellent article. Really good resource for those learning to use flex in building facebook apps! Alex
Thu, 03/27/2008 - 10:37 — Anonymous (not verified)
keep "com" folder in your flex builder application..
Sun, 04/20/2008 - 17:58 — Anonymous (not verified)
Is there any reason for the change from: com.terralever.facebook. to com.pbking.facebook ? Just curious... -R
Tue, 07/08/2008 - 13:47 — kumar (not verified)
how to get the friend's mobile number and other number through this API.
Thu, 09/04/2008 - 14:31 — Anonymous (not verified)
Hi, I am trying to run this tutorial but getting following errors. I did same thing as mentioned in above steps but no luck. 2. Severity and Description Path Resource Location Creation Time Id Can any one help me to make it work. Many Thanks Post new comment |
||||||