To get to know much about developing Facebook applications using a server side, I have chosen with Koala for Rails. For .NET, Facebook C# SDK is being well supported by Microsoft also (http://facebooksdk.codeplex.com/). In this post, I explain how to use Koala in your Rails applications. As any Facebook app, the prerequisite are:
- Facebook account (of course you should be the one in the 10% of world population )
- A Facebook application (which could be in sandbox mode), so you should have App ID, App Secret and Callback URL
- A Rails application in your local machine and the Facebook app pointed to this as http://localhost:3000 (default port for Mongrel or WEBrick)
Installing Koala
Install Koala gem from https://github.com/arsduo/koala by using sudo gem install koala –pre
Configuring Koala
Create facebook.yml in /config with the following content
1 2 3 4 5 6 7 8 9 |
|
Add the following in config/environment.rb
1 2 3 4 |
|
The first line enables Facebook callback to your server. The following line adds koala gem into this application.
Create koala.rb in /config/initializers with the following content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The above code loads the Facebook.yml settings into Facebook, so you can access AppID, AppSecret and CallbackURL anywhere in your application in an unified way. The following OAuth extension method is taken from Koala guidance to simplify the instantiation of OAuth.new.
Controller Part
In ApplicationController, add the following
1 2 3 4 5 6 7 8 |
|
Note that protect_from_forgery has been commented. The following instructions, let this application load OAuth details from cookies for getting Facebook access token.
Your Login Page
Add the following on your login page layout, in this case /app/views/layout/login.html.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
In this example, I’ve used Facebook JavaScript SDK for login screen. To use this, I’ve include FBML scheme in as xml:fb=”http://www.facebook.com/2008/fbml” followed by referring Facebook SDK script file.
You Home Page
In your home page, add the following things.
In app/controllers/HomeController.rb
1 2 3 4 5 6 |
|
In the above code, new Graph instance has been created. By using this, I’ve invoked the currently logged in user’s likes.
In app/views/home.html.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Now, run your application. Hope it would be easy.