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.
This tutorial series steps you through the process of building and deploying desktop and web applications that retrieve, display, and post Facebook data. In Part 3, you built and deployed a web application on your own web server. In this part, Part 4, you will modify the web application so it can also be accessed on the Facebook website.
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.
Start by editing the application's properties in the Facebook Developer application so that it can be accessed on the Facebook website.
Assign a Facebook URL for a page that loads the application from your server into an iFrame in a Facebook web page.
This application has already been authorized with Facebook (when you logged into it as a external web application) so to test it as a web application on Facebook.com, you need to first de-authorize it.
Login functionality is provided by the Facebook init() and login() methods just as it was in the external web appliction with one difference: if the user is not logged in or the application has not been authenticated, you want to redirect the browser window to the Facebook log in and/or authentication pages instead of displaying them in pop-up windows. To accomplish this, you will determine if the application is being acessed on the Facebook website and if it is, then when a user needs to log in or authorize the application, you call a JavaScript method in the wrapper page to redirect the browser.
To complete this tutorial, you need a working web application as it was at the end of Part 3.
If the application is accessed on the Facebook website, it does not need to contain login and logout buttons. Make two new application states, loggedinonfacebook and loggedoutonfacebook, that do not contain the buttons.
When an application is accessed on the Facebook website, it is loaded into an iFrame. You can determine if the application has been loaded into an iFrame or not by using JavaScript to check the URL of the top window. If you assign a variable the value of the top window location, it will be assigned a value when the application is acessed on your own server but will not have a value when the application is accessed on the Facebook website.
topURL of type String.protected var topURL:String;
call() method of Flash Player's ExternalInterface class to execute some JavaScript to retrieve the URL of the top window.protected var topURL:String=ExternalInterface.call('top.location.toString');
application1_creationCompleteHandler(), set the currentState so that it is set to loggedout or loggedoutonfacebook depending upon if the user is accessing the application on Facebook or not.protected function application1_creationCompleteHandler():void
{
Facebook.init("YOUR APPLICATION ID HERE",loginHandler);
currentState = (topURL) ? "loggedout": "loggedoutonfacebook";
}
This code uses the ternary operator for brevity. The equivalent code using if/else statements would be the following:
if (topURL){
currentState="loggedout";
}
else{
currentState="loggedoutonfacebook";
}
loginHandler(), change the currentState assignment so that it is set to loggedin or loggedinonfacebook depending upon if the user is accessing the application on Facebook or not.protected function loginHandler(success:Object,fail:Object):void
{
if(success){
currentState = (topURL) ? "loggedin": "loggedinonfacebook";
Facebook.api("/me",getMeHandler);
//more code
Lastly, modify the login code to redirect the browser to the Facebook login and/or authorization pages when the user is accessing the application on Facebook.com.
redirect() function to the script block. It redirects the browser window to the Facebook authorization page and passes to it as URL variables the application ID (the client ID), a list of extended permissions, the redirect URI (the URL for your application on Facebook), and all other existing URL variables.function redirect(id,perms,uri) {
var params = window.location.toString().slice(window.location.toString().indexOf('?'));
top.location = 'https://graph.facebook.com/oauth/authorize?client_id='+id+'&scope='+perms+'&redirect_uri='+uri+params;
}
loginHandler(), add an else if block that checks to see if the login failed and if topURL is not defined.protected function loginHandler(success:Object,fail:Object):void
{
if(success){
//existing code
}
else if(!success && !topURL){
}
}
redirect() method in the index.html wrapper file and pass to it the application ID, the list of extended permissions, and the Canvas Page URL for your application on Facebook.else if(!success && !topURL){
ExternalInterface.call("redirect","YOUR APPLICATION ID HERE", "user_birthday,read_stream,publish_stream","http://apps.facebook.com/stallonsuserstatus/");
}
Note: Because the application ID is now being used in two places, you may want to define a variable to hold its value and then use that in the various places in your code.
You are now ready to upload and run your application on Facebook and debug it with Flash Builder.
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.
application1_creationCompleteHandler() function (see Figure 15).
topURL variable anywhere in the code and select Create Watch expression.
topURL; it should be null (see Figure 17).
topURL; it should no longer be null (see Figure 19).
A normal development workflow for working with a Flex and Facebook web application on Facebook 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.
In this tutorial, you modified a Flex and Facebook external web application so that it could also be accessed on the Facebook website. The first step was to edit the application's settings on Facebook.com and assign it a Canvas Page and Canvas URL. Next, you modified the existing web application code. You created two new application states that did not contain login or logout buttons for use when the application is accessed on the Facebook website. You used JavaScript to check the value of top.location to determine from where the application is being accessed. You modified the login functionality so that if the application is accessed on Facebook.com, the browser is redirected to the Facebook login and/or authentication pages instead of displaying them in pop-up windows. And finally, you deployed the application to your server and then browsed to and debugged the application on Facebook.com.
For examples and details of applications using additional types of interaction with Facebook, see the Facebook Graph API documentation and sample code.
To learn more about creating and customizing Flex applications, see the Flex documentation and the Adobe Flex Developer Center.