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)
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
# 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.
Controller Part
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
Udooz Sample <%= flash[:notice] %>
udooz
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
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.
In app/views/home.html.erb
<%=h like["name"]%> |
<%=h like["category"] %> |
– |
Now, run your application. Hope it would be easy.
Joe
Feb 11, 2011 @ 01:05:01
Thank you very much! So helpful
Nicolas
Mar 14, 2011 @ 14:59:36
Hi,
my /environment.rb is like this :
# Load the rails application
require File.expand_path(‘../application’, __FILE__)
# Initialize the rails application
Web::Application.initialize!
config.action_controller.allow_forgery_protection = false
config.gem “koala”
and when I try to run my server, I got the following errors :
=> Booting WEBrick
=> Rails 3.0.4 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/local/lib/ruby/1.9.1/syck.rb:145:in `initialize’: No such file or directory – /config/facebook.yml (Errno::ENOENT)
from /usr/local/lib/ruby/1.9.1/syck.rb:145:in `open’
from /usr/local/lib/ruby/1.9.1/syck.rb:145:in `load_file’
from /home/nicolas/web/config/initializers/koala.rb:4:in `’
from /home/nicolas/web/config/initializers/koala.rb:3:in `’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:235:in `load’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:235:in `block in load’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `block in load_dependency’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:596:in `new_constants_in’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:225:in `load_dependency’
from /home/nicolas/web/devise/gems/activesupport-3.0.4/lib/active_support/dependencies.rb:235:in `load’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/engine.rb:201:in `block (2 levels) in ‘
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/engine.rb:200:in `each’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/engine.rb:200:in `block in ‘
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `instance_exec’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/initializable.rb:25:in `run’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/initializable.rb:50:in `block in run_initializers’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `each’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/initializable.rb:49:in `run_initializers’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/application.rb:134:in `initialize!’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/application.rb:77:in `method_missing’
from /home/nicolas/web/config/environment.rb:5:in `’
from /home/nicolas/web/config.ru:3:in `require’
from /home/nicolas/web/config.ru:3:in `block in ‘
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/builder.rb:46:in `instance_eval’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/builder.rb:46:in `initialize’
from /home/nicolas/web/config.ru:1:in `new’
from /home/nicolas/web/config.ru:1:in `’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/builder.rb:35:in `eval’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/builder.rb:35:in `parse_file’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/server.rb:162:in `app’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/server.rb:248:in `wrapped_app’
from /home/nicolas/web/devise/gems/rack-1.2.2/lib/rack/server.rb:213:in `start’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/commands/server.rb:65:in `start’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/commands.rb:30:in `block in ‘
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/commands.rb:27:in `tap’
from /home/nicolas/web/devise/gems/railties-3.0.4/lib/rails/commands.rb:27:in `’
from script/rails:6:in `require’
from script/rails:6:in `’
It seems it does not recognize the config object in the environment.rb file.
Thanks for your help,
Regards
Nicolas
Apr 06, 2011 @ 23:24:55
CONFIG = YAML.load_file(Rails.root + “/config/facebook.yml”)[Rails.env]
putting
#{Rails.root}
in this line got me a little further. now I have this error:
../initializers/koala.rb:6: syntax error, unexpected tCONSTANT, expecting ‘)’ (SyntaxError)
FB_SECRET = CONFIG['secret_key']
Apr 07, 2011 @ 01:31:50
“/config/facebook.yml” needs to be “config/facebook.yml” or else it breaks.
Chris Baclig
May 18, 2011 @ 01:37:41
I stumbled upon your blog on Google, and I’m glad to hear that you chose Koala to start experimenting with Facebook applications! If I may ask, what made you decide to use Koala over any other Facebook Ruby libraries?
By the way, Koala is now officially on version 1.0 and the recommended way to install it is using “sudo gem install koala” (without the –pre option.)
Cheers,
Chris
udooz
Jun 14, 2011 @ 15:12:36
Thanks Chris…. I will provide the reason for choosing “Koala” in another post.
logan
May 29, 2011 @ 12:56:05
hi,
I’m new to rails ,how to set the routes for this application ?
dbKooper
Jul 27, 2011 @ 02:16:32
NoMethodError in HomeController#index
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Application Trace | Framework Trace | Full Trace
app/controllers/home_controller.rb:3:in `index’
pankaj
Aug 15, 2011 @ 19:59:44
OAuthException: (#200) The user hasn’t authorized the application to perform this action
reduce electricity
Oct 08, 2011 @ 22:40:58
Thanks ! Supper Post !
Christian
Oct 09, 2011 @ 14:06:19
If you wanted to Save some Information belonging to this logged in user. How would you do it?
Whould the user model only contain the facebook id?
and lets say some gamestats belong to this facebook id?
udooz
Oct 09, 2011 @ 18:43:40
It varies based on your app nature:
* use facebook id as one of your user ID and for the internal data store access, use respective database primary key ID.
Rina Noronha
Oct 13, 2011 @ 02:37:36
Hi, udooz!
I’m the web editor at iMasters, one of the largest developer communities in Brazil. I´d like to talk to you about republishing your article at our site.
Can you contact me at ?
Ugg Adirondack II
Oct 14, 2011 @ 18:57:40
I do not ordinarily comment but I gotta say regards for the article on this one.
Oct 15, 2011 @ 16:54:39
[...] Rate this: Share this:TwitterFacebookEmailPrintDiggLinkedInRedditStumbleUponLike this:LikeBe the first to like this post. [...]