26 September 2011
A basic knowledge of web technologies and object-oriented programming principles is helpful.
You also need:
- A Facebook account: Sign up
- A registered Facebook application: See Part 1
- ActionScript 3 SDK for Facebook Platform
- A publicly available web server (not localhost) for testing a Flex and Facebook web application.
Beginning
Note: This tutorial series was updated September 2011 to use the Adobe ActionScript 3 SDK for Facebook Platform, which uses OAuth 2.0. Facebook is requiring all applications to use OAuth 2.0 by October 1, 2011.
Note: In September 2011 Facebook partnered with Heroku to provide free hosting for Facebook sample apps using PHP, Ruby, Node.j, or Python. You can select any of these environments, since this tutorial does not use any server-side code.
This tutorial series steps you through the process of building and deploying either a desktop or web application that retrieves, displays, and posts Facebook data. In Part 1, you built a local Flex and Facebook desktop application with Flash Builder and the Adobe ActionScript 3 SDK for Facebook Platform. In this part, you will modify the local desktop application you built in Part 1 and deploy it as a web application on your own web server.
You are modifying an existing desktop application instead of building a web application from scratch for two reasons. One, you cannot test Facebook web applications locally; they have to be accessed on a public web server and registered with Facebook in order to work. Thus, it is much easier to first build and debug a local desktop application. Second, most of the code for the desktop and web application is the same, so it would be redundant to step through them both. Instead, you will just modify the desktop application.
Specifically, in this tutorial you will:
Note: Facebook frequently changes to the site's user interface and API. This makes it difficult to keep this tutorial 100% up-to-date. Please be sure to add and read the comments at the end of this tutorial for any user interface or API modifications that might have occurred.
Make sure you have a working local desktop application and then create a web project that starts with that desktop code.
To complete this tutorial, you need a working desktop application as it was at the end of Part 1.
Because you cannot convert a Flash Builder desktop project to a web project, you next need to make a new web project.
Next, download the Adobe ActionScript 3 SDK for Facebook Platform and add it to your project.
Note: If you want to look at the source code, download the source code zip instead (or in addition to the SWC). After unzipping it, copy the com folder and add it to the src directory of your project in Flash Builder. You can then browse the source code for the classes contained in the SWC.
Because most of the code for the desktop and web applications will be the same, start with the existing desktop code in your web project.
windowedapplication1_creationCompleteHandler() function to application1_creationCompleteHandler.creationComplete event to the Application tag and set it equal to application1_creationCompleteHandler(event).<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
minHeight to 300 and the minWidth to 600.To provide Facebook login and authorization functionality in web applications, you use the Facebook class (com.facebook.graph.Facebook) instead of the FacebookDesktop class (com.facebook.graph.FacebookDesktop). The code you write using these classes is very similar; they both have init() , login() , and logout() functions.
In your code, change the FacebookDesktop references to Facebook.
The FacebookDesktop logout() method has no arguments; the Facebook class's logout() method requires one, a callback function to call when the user logs out.
logout() function.logout() method of the com.facebook.graph.Facebook class (see Figure 3). Unlike the FacebookDesktop logout() method which has no required arguments, the Facebook logout() method has one required argument, a function to call when the Facebook logout succeeds.
logout() method a reference to a function logoutHandler.Facebook.logout(logoutHandler);
logoutHandler() with one argument (an Object) and no return value.protected function logoutHandler(response:Object):void{
}
You can run the application now, but nothing will happen when you click the login button. The login() method of the Facebook class, calls a method in the Facebook JavaScript SDK, which has not been included in the wrapper page yet.
You need to make several changes to the login code. The FacebookDesktop login() method has a second optional argument equal to an array of extended permissions; the optional second optional argument of the Facebook class's login() method is an object. There is also a difference in what login functionality each class provides. In addition to handling user login and application authorization, the FacebookDesktop login functionality also makes an initial Facebook API call (to the me object) to retrieve all the basic information about the user. The login functionality of the Facebook class does not make this initial call so you need to modify your code to add it.
login() method of the com.facebook.graph.Facebook class (see Figure 4). Unlike the FacebookDesktop login() method which has a second optional argument equal to an array of extended permissions, it has a second optional argument equal to an object that contains information for modifying the login window.
login() function, change the second argument of the Facebook login() method from an array to an object with a property called scope (the name required by the Adobe ActionScript 3 SDK for Facebook Platform) equal to a comma-delimited list of the extended permissions.Facebook.login(loginHandler,{scope:"user_birthday,read_stream, publish_stream"});
loginHandler(), use the Facebook api() method to call the Facebook Graph me object and specify a function getMeHandler to be the callback function.protected function loginHandler(success:Object,fail:Object):void
{
if(success){
currentState="loggedin";
Facebook.api("/me",getMeHandler);
nameLbl.text=success.user.name;
//more code
getMeHandler() with two Object arguments and no return value.protected function getMeHandler(result:Object,fail:Object):void{
}
nameLbl and birthdayLbl from inside loginHandler() to inside getMeHandler() .protected function getMeHandler(result:Object,fail:Object):void{
nameLbl.text=success.user.name;
birthdayLbl.text=success.user.birthday;
}
success.user with result .nameLbl.text=result.name;
birthdayLbl.text=result.birthday;
The current wrapper page for the application (FacebookUserStatusWeb.html) is an HTML page that was generated automatically by Flash Builder. It contains the code to check for the correct version of Flash Player needed to run your application and embeds your application. The JavaScript Flash Player detection code is provided by the open-source SWFObject.
Note: You can learn more about SWFObject from documents on the Google Code repository and the article, Detecting Flash Player versions and embedding SWF files with SWFObject 2.
When you deploy your application as a web application, the wrapper page needs some additional functionality to provide a seamless user login experience—one in which the Facebook login and/or application authorization pages appear in pop-up windows. To provide this experience, the login functionality for your application will be handled by the Facebook JavaScript SDK. The init() , login() , and logout() methods of the Facebook class in the Adobe ActionScript 3 SDK for Facebook Platform call methods of the com.facebook.core.FacebookJSBridge.as class which in turn call methods of the Facebook JavaScript SDK.
A wrapper file is provided for you in this tutorial. It is a modifed version of an index (dynamic).html sample file located in the Embedding folder of the GraphAPI_Examples.zip which you can download from the Google code respository. In order to keep the wrapper page server-agnostic, you will use an HTML page.
If you ran the application now, the SWF file would open in the operating system's default application for viewing SWF files. You could create a run configuration in Flash Builder to launch your new index.html wrapper page instead of FacebookUserStatusWeb.html, but this would do you no good because the application must live on a publicly available web server that you specify in the application's Facebook settings to work.
In order to run and debug the web application, you must first upload it to your web server.
Note: Later in the tutorial after you are finished testing and debugging the web application, you will create and upload a release version of the SWF to replace this debug version. At that time, you can also remove the Flex framework SWF files from the server.
Note: You could also just browse to the application URL directly in a browser window; for example, browse to http://stallons.com/facebook/userstatusweb/.
Next, edit the application's properties in the Facebook Developer application so that it will work as an external web application and then browse to the application.
You need to register the URL for you web application with Facebook so that Facebook will enable authentication for it.
This application has already been authorized with Facebook (when you logged into it as a desktop application) so to test the web application, you need to first de-authorize it.
You are now ready to run and debug your remote web application with Flash Builder.
If you were already logged in to Facebook, you would have got this dialog box directly.
getMeHandler() function.Your application will launch in a browser window as before, but now the URL has ?debug=true appended to it; for example, http://stallons.com/facebook/userstatusweb/?debug=true. If you are already logged in to Facebook, application execution is suspended and you will automatically be returned to Flash Builder. If you are not logged in, click the login button, log in, and then return to Flash Builder.
A normal development workflow for working with a Flex and Facebook web application is the following:
Once you are done testing and debugging the application, you should create and upload a release version of the application SWF file. The SWF file that has been generated up to now (and stored in the bin-debug folder and uploaded to your web server) contains extra information for debugging the application during development.
Note: Instead of uploading the entire folder, you can just upload the release version of FacebookUserStatusWeb.swf. The images folder is not needed; it contains some images that were compiled into the application. The index.html file is already on the server and has not been changed. The SWZ files are the signed Adobe runtime shared library files containing all the framework code. The first time a user loads a Flex application using a certain version of the SDK, the SWZ files are downloaded from the Adobe website and stored on the user's computer for future use by this application and other Flex applications using the same version of the SDK. By default, the backup or failover location of these SWZ files (in case the Adobe server is down) is set to be the folder on your web server where the SWF lives. The release version of the application uses these SWZ files, not the SWF files used by the debug version, so if you want, you can also delete the Flex framework SWF files from the server.
In this tutorial, you used the Adobe ActionScript 3 SDK for Facebook Platform in conjunction with the Facebook JavaScript SDK to provide the login and authorization functionality for an external Flex and Facebook web application. You started with a local desktop application, converted it into a web application, and then uploaded it to a web server. In order for the application to work, you had to register the website URL with Facebook using the Facebook Developer application.
To deploy the application as a web application on Facebook.com, continue on to Part 4 of this tutorial series, Modify and deploy on Facebook.com.