Visit our archive

Over the past couple of years, we have created dozens of Facebook applications, page tabs, etc. It seems like everyone wants to do something new and different with Facebook. A lot of the time the projects have been built with Flash / AS3 which means that there is no code support on Facebook’s developer site. Fortunately Adobe and Facebook have worked together to create an API that can be used quite easily in Flash, there are also version of the API available that work very similarly for both Mobile and Desktop applications created with Adobe AIR.

To get the API, go to http://code.google.com/p/facebook-actionscript-api/ and download the latest version. Before a project, we generally just update the SVN repository with the latest code and make a copy to the project folder. This ensures two things:

  1. We are working with the latest version.
  2. the fact that we copied the code to the project folder means that we can pass the code along between developers and there won’t be an issue with functionality.

We are writing this post, not because the API is overly complex or difficult to understand and use, but because every time we start a project our first instinct is to search the web for a starting point to use the API. Today, we were doing just that when we realized that there are tons of tutorials around the web BUT they all share the same flaw – they were built with various different versions of the API where connect to Facebook’s Graph API via the facebook-actionscript-api. Meaning that the connection doesn’t always work as expected when using someone else’s code. This is part of what we meant above when we mentioned copying the current version to the project folder so that the calls are always going to be the same.

Now, how do you get to know the latest way to connect and use the API? The examples that are provided on the Google Code page and in the repository are perfect for that! They connect using all the latest methods, so you no longer have to go looking around the web for tutorials that may or may not work for the version that you have.

We are working with GraphAPI Web 1.8.1 today. The examples that come with it, include one called “FlashWebExample”. It covers all the need-to-knows with regards to connecting to Facebook through the API and making any calls you may need. Here is a quick example with some descriptions along the way:

//Initialize Facebook library
// this does NOT pop up any dialogs, it just checks the login status of the user with YOUR app
// the APP_ID is a constant set to your application's id 
// onInit is the call back function that gets fired when the API gets a response 
Facebook.init(APP_ID, onInit);

The example’s onInit function is very simple, we have changed a couple things around here because we don’t want a login button, we want it to try to connect automatically. What this does is checks to see if the user is logged in and then functions accordingly

// result is the success object, fail is a fail object
protected function onInit(result:Object, fail:Object):void {						
	if (result) { //already logged in because of existing session
		trace("onInit, user is logged in");
	} else {
		trace("onInit, user is not logged in");
		// the opts are the scope you require for your application
		var opts:Object = {scope:"publish_stream, user_photos"};
		// this calls the Facebook login function and sets the call back to onLogin
		// this should pop up the required dialogs for the user and return when done
		Facebook.login(onLogin, opts);
	}
}

So far we have just re-arranged a lot of the code within the API’s example! That was super easy, haven’t really needed to write any code for ourselves. Next we are going to handle the feedback from the login call, if the user logs in we will call the Graph API to get the user info (for a full list of calls see the Facebook Developer site):

// called when the login dialogs are completed
protected function onLogin(result:Object, fail:Object):void {
	if (result) { //successfully logged in
		// now the user is logged in and 
		trace("logged in");
		// call the API to get the user's info, we will show you what goes in onCallApi next
		Facebook.api("/me", onCallApi);
	} else {
		trace(“login failed”);
	}
}

So now you have checked for the user’s connect, logged them in and requested their basic Facebook information. You just need to do something with it. The first thing that you need to know is that the information is going to be returned to your application in JSON format. This is nice, quick, text-based data; the API happily comes with the necessary library for Flash that parses the JSON into a Flash object for you. This is done in the onCallApi method below:

// this function is fired when Facebook responds to your API request above (“/me”)
protected function onCallApi(result:Object, fail:Object):void {
	// if there is a result that means that the call was successful
	if (result) {
		// trace out the name of the user that was returned and then a JSON string with the rest
		trace("RESULTS", result.name, JSON.encode(result));
	} else {
		// there was an error, lets find out what it was
		trace("ERROR", JSON.encode(fail));
	}
}

That is it ! You have successfully connected to Facebook’s Graph API without having to write any of your code. There are a couple of things to remember:

  1. this is for a web application, meaning that you can only test it in the browser.
  2. check the version of the API before deciding that your application isn’t working as we have shown above.
share save 171 16 Facebook ActionScript API
  • iphone mafia Jan 2, 2012

    Generally I don’t learn article on blogs, however I would like to say that this write-up very forced me to take a look at and do it! Your writing taste has been surprised me. Thanks, quite great article.

  • web directory Feb 1, 2012

    I don’t even know the way I stopped up here, but I assumed this post was great. I don’t realize who you’re however definitely you are going to a well-known blogger if you happen to are not already. Cheers!

  • You can definitely see your enthusiasm in the paintings you write. The arena hopes for even more passionate writers like you who are not afraid to mention how they believe. At all times follow your heart.

  • URL Mar 10, 2012

    … [Trackback]…

    [...] Informations on that Topic: cultcreative.com/tutorials/11/11/2011/facebook-actionscript-api [...]…

  • karthickkarthick Mar 23, 2012

    i am getting the following error
    {
    “error”: {
    “message”: “An active access token must be used to query information about the current user.”,
    “type”: “OAuthException”,
    “code”: 2500
    }
    }

    and i am getting the following error when i press login

    Error opening URL ‘https://graph.facebook.com/me’

  • cultcreative Mar 26, 2012

    @karthickkarthick make sure that you setup the app and you are testing from the domain that you set the app up with.

  • Reference Apr 6, 2012

    I savor, lead to I discovered exactly what I used to be taking a look for. You have ended my 4 day long hunt! God Bless you man. Have a great day. Bye

  • website maken Apr 24, 2012

    Hi…..

    I’ve recently started a website, the information you provide on this web site has helped me tremendously. Thank you for all of your time & work. “You do ill if you praise, but worse if you censure, what you do not understand.” by Leonardo DaVinci….

  • tsx venture 50 Apr 29, 2012

    Hello there, simply turned into aware of your blog thru Google, and found that it is really informative. I’m gonna watch out for brussels. I will appreciate if you continue this in future. A lot of other folks will be benefited from your writing. Cheers!

  • turban May 2, 2012

    Thank you for every other magnificent post. Where else may just anybody get that kind of information in such an ideal method of writing? I have a presentation next week, and I am at the look for such information.

  • law of attraction May 18, 2012

    I just like the valuable information you provide for your articles. I will bookmark your blog and take a look at once more here regularly. I am slightly sure I’ll be informed many new stuff right right here! Good luck for the next!

  • My Homepage Dec 2, 2012

    … [Trackback]…

    [...] Read More: cultcreative.com/tutorials/11/11/2011/facebook-actionscript-api/ [...]…

  • Mckinley Delapena Jan 23, 2013

    The Silent Shard…

    This tends to likely be very helpful for many of the positions I decide to never only with my website but…

  • Name (Required)

  • Email (Required, but not published)

  • Url (Optional)

  • Comment (Required)

*

Current month ye@r day *