Wednesday, July 16, 2008

ABC of Facebook Application

Facebook offers a great platform and userbase for developing your cool app, but on the other hand their documentation is useless specially in Java side. I can' find words how badly it is written !*&@*&#*#.
When I development of my first Facebook app, I struggled a lot. I could find much more information in people's blog rather than on Facebook website.

I'll cover few of topic which will app development little simpler.

Facebook Session Demystified
Once user click on your application canvas link Facebook call your callback URL and pass this response to user browser. In this call authentication token is NOT added in the request your server receives. If you need authentication token you need to forward request to following URL

Don't worry users will not be asked to login again. Facebook will create an authentication token, append it in your callback URL and send request to this URL.

Now you have authentication token. Store it in HttpSession; your will need it soon...
I a second we would try to fetch user friends list using FacebookRestClient class. I am using Facebook java apis hosted at
Use the stored authentication token to create instance of FacebookRestClient as

FacebookRestClient fClient = new FacebookRestClient(YOUR_API_KEY,YOUR_SECRET_KEY);
String fbSession = fClient._getSessionKey();
long fbUserId = fClient.auth_getUserId(token);

After this step most of the things are simple. Use various methods of FacebookRestClient to perform various actions e.g. getting User friend list.

List fList = fClient.friends_getAsList();

Store fbSession in HttpSession and use it in following request to get handle of FacebookRestClient in following way

FacebookRestClient fClient = new FacebookRestClient(YOUR_API_KEY,YOUR_SECRET_KEY,fbSession);

In next few post I'll try to cover more topics about Facebook Api including Feeds, notifications and more.

No comments: