Feb 7 2011
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 (). 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)
Install Koala gem from https://github.com/arsduo/koala by using sudo gem install koala –pre
Create facebook.yml in /config with the following content
# config/facebook.yml development: app_id: 184xxxxxxxxxxxx secret_key: 80xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx callback_url: http://localhost:3000/ test: ... production: ...
Add the following in config/environment.rb
# config/environment.rb # in Rails::Initializer.run do |config| config.action_controller.allow_forgery_protection = false config.gem "koala"
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
# config/initializers/koala.rb module Facebook CONFIG = YAML.load_file(Rails.root + "/config/facebook.yml")[Rails.env] APP_ID = CONFIG['app_id'] SECRET = CONFIG['secret_key'] CALLBACK_URL = CONFIG['callback_url'] end Koala::Facebook::OAuth.class_eval do def initialize_with_default_settings(*args) case args.size when 0, 1 raise "application id and/or secret are not specified in the config" unless Facebook::APP_ID && Facebook::SECRET initialize_without_default_settings(Facebook::APP_ID.to_s, Facebook::SECRET.to_s, Facebook::CALLBACK_URL.to_s) when 2, 3 initialize_without_default_settings(*args) end end alias_method_chain :initialize, :default_settings end
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.
In ApplicationController, add the following
#app/controller/application.rb # protect_from_forgery before_filter :parse_facebook_cookies def parse_facebook_cookies @facebook_cookies = Koala::Facebook::OAuth.new.get_user_info_from_cookie(cookies) end
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
<%= flash[:notice] %>
You Home Page
In your home page, add the following things.
class HomeController < ApplicationController def index graph = Koala::Facebook::GraphAPI.new(@facebook_cookies["access_token"]) @likes = graph.get_connections("me", "likes") end end
In the above code, new Graph instance has been created. By using this, I’ve invoked the currently logged in user’s likes.
|<%=h like["category"] %>|
Now, run your application. Hope it would be easy.