Home > Flex > Creating Facebook application with Flex

Creating Facebook application with Flex

September 6th, 2009 brupp Leave a comment Go to comments

The article will help you create your first Facebook external website application using Flex. Assuming you have Facebook account; go to http://www.facebook.com/developers/createapp.php and create a new application. This is what you will see:

edit_page

Note down the API Key and Secret, you will need these later. There are number of edit options that you can play around with. However for this article you now need to go to “Advanced” tab and select “Application Type” to “Desktop”. Why so? because that would make your application easier to test and debug at time of development.

Create a new project using Flex Builder. Now you need Actionscript 3.0 client library for Facebook Platform API. Get it from here: http://code.google.com/p/facebook-actionscript-api/ . Deploy it in your project either the “swc” in lib folder or the source “com” folder in “src”.

The stage is now set. For sake of example I am taking a simple application which will show up user’s Facebook friends in a Datagrid. Now in order to access your profile information, the app first need to connect with user’s Facebook account. So either at launch of your app you need to do that or have a button to take user to that.

fb_app

Before you go any further please download the fb_app.mxml.

Lets get into how the code flow:

1- Clicking on “Click to login…” button, a facebook session needs to be created for this app.

session = new FacebookSessionUtil(API_KEY , SECRET_KEY , this.loaderInfo);
session.addEventListener(FacebookEvent.CONNECT, onConnect);
fbook = session.facebook;
session.login();

2- Now the user will be taken to login page (new window) of his/her account to allow the app to access the user’s data.

3- Once the app is “Allowed ” user has to go back to the app page. Here’s a catch how the app will know that user has allowed and session is valid.

4- When user clicks “Show My Friends…” list session validation is done:

session.validateLogin();

5- Only when the validation is done FacebookEvent.CONNECT is fired.

6- Now you can play with the user’s data in whatever clever way you can. To access any data now the flow is to make a call.

var call:FacebookCall = fbook.post(new GetFriends(null, fbook.uid));
call.addEventListener(FacebookEvent.COMPLETE, onGetFriends);

post method accepts a facebook command. Check out  com.facebook.commands.* for various commands to retrieving album, friends etc.

7- The response will be of type “FacebookData”.

8- Every facebook user has a unique “uid” and thats the key to get the data.

Your first facebook app is up and running.

If you liked this post, please spread the word. Share and cheer!!!.

If you need more information on above post drop a comment.

Categories: Flex Tags: ,
  1. October 22nd, 2009 at 07:56 | #1

    I've been working with 3.0 library and it's really easy to use, but I'm having trouble finding decent examples for 3.2 library.

    Is there any good example how to use Facebook Library 3.2? I tried to create the same functionality with this library, but no use :(

  2. October 22nd, 2009 at 17:28 | #2

    Hi Ivan, version 3.3 is released, try it out. Let me know if that solves the issues you are facing in 3.2.

  3. Satiya
    November 19th, 2009 at 16:41 | #3

    Hi,
    Im new to using as3 but have used php to make FB apps so far.. I just have a simple question. Is it possible to use a web-based app using flash ? if so can u post the most basic example.

    Thanks

    • November 19th, 2009 at 16:52 | #4

      Yes Satiya, it is not only possible to create web apps with flash/flex using AS3.0 but it is the most happening technology for creating web apps these days. As for your question the above application runs in browser however if you want to port this inside FB you need to change the configuration in FBs “Advanced” tab “Application Type” from “Desktop” to web based.. (or similar word check it out).
      Keep Exploring!

  4. NewBee
    January 28th, 2011 at 17:26 | #5

    I am completely new to this.. n i am facing one error.. "Adobe AIR application is missing the corresponding fb_app-app.xml."
    Have any idea where i went wrong or where i missed something??

  1. No trackbacks yet.