Flash, Facebook Graph API in tabs
I am by no means an expert at developing for Facebook, and to be honest neither would I want to be. What little I have done on the Facebook platform, I’ve found myself getting frustrated with the confusing, out-of-date and often half-baked documentation.
At last it seems like Facebook might be getting their acts together and the Graph API looks impressive and simple to use.
I’ve been working on a project where I had to get a users basic information within flash in a Tab and I decided to use the Graph API.
Authentication
The Graph API requires an OAuth Access token, but by default in a tab you are provided with a session key which is passed along with the rest of the Facebook variables as a flashVar. But fear not, Facebook provide a service to convert a session key into a OAuth Access Token, which in AS3 looks like this:
var variables : URLVariables = new URLVariables(); variables["type"] = "client_cred"; variables["client_id"] = __appID; variables["client_secret"] = __secret; variables["sessions"] = __sessionKey; var urlRequest : URLRequest = new URLRequest(EXCHANGE_SESSIONS_URI); urlRequest.method = URLRequestMethod.POST; urlRequest.data = variables; var urlLoader : URLLoader = new URLLoader(); urlLoader.load(urlRequest);
Once you’ve successfully got your OAuth Access Token you can start using the API.
Fetch data
var uri:String = REQUEST_URI + __id + (__connection.length > 0 ? "/" + __connection : "");
var variables : URLVariables = new URLVariables();
if(_accessToken) variables["access_token"] = _accessToken;
if(__filters)
{
for (var i : String in __filters)
{
variables[i] = __filters[i];
}
}
var urlRequest : URLRequest = new URLRequest(uri);
urlRequest.method = URLRequestMethod.GET;
urlRequest.data = variables;
var urlLoader : URLLoader = new URLLoader();
urlLoader.load(urlRequest);
Load picture
var uri:String = REQUEST_URI + __id + "/picture" + "?access_token=" + _accessToken + (__size.length > 0 ? "&type=" + __size : ""); var loader:Loader = new Loader(); loader.load(new URLRequest(uri));
Search
var uri:String = REQUEST_URI + "/search"; var variables : URLVariables = new URLVariables(); if(_accessToken) variables["access_token"] = _accessToken; variables["q"] = __q; variables["type"] = __type; var urlRequest : URLRequest = new URLRequest(uri); urlRequest.method = URLRequestMethod.GET; urlRequest.data = variables; var urlLoader : URLLoader = new URLLoader(); urlLoader.load(urlRequest);
I’ve created a utility class to make using the Facebook Graph API easier.