Ruby on Rails and Facebook API (Koala): Basic Example

Talking to a developer today about getting her toes wet in Ruby and Facebook. I wrote up this simple example of how to set it up.

First, Koala is a lightweight, transparent library for Facebook API calls. I’m pretty happy with it- the developer is very responsive and since I know the Facebook API, it’s nice that there’s not much burdensome renaming and such.

So, this is for Rails 3.
1. Install on local system the Koala gem:

sudo gem install koala –version=1.2.1

Add to Gemfile:

gem ‘koala’, ‘1.2.1’

Hey, we’re on Rails, why don’t you use the Koala-Rails gem! Well I did, but now I don’t because it’s not up-to-date, mainly in accordance with the latest Koala gem, which had some nifty photo upload-from-AWS abilities.


Authentication (“oauth/auth”) is always the first step. Check out this reference: “Koala on Rails” on the wiki (don’t be confused- it’s not about the gem koala-rails, but about Koala ON Rails). Anyway, In that post, I do “via redirects” because- and this is especially relevant for Facebook iFrame app developers– browsers won’t allow third parties cookies, so all of the cookie management and auth stuff is relatively useless. I use the “url_for_oauth_code” method in Koala to get the right authentication url, have the user click to it, and in the return trip, grab the access token with the “get_access_token” method- and save it to a session variable. I leave it to Rails to deal with the hows and whats of session management.


First, In config/initializers/constants.rb, setup some constants for your key Facebook app info- the APP_ID, SITE_URL, etc. all cribbed from the Facebook application page. Note: the REDIRECT_URI will be the SITE_URL + “/callback”. The callback method handles the goods once Facebook returns from its authentication journey.

Then, create a few methods (remember to add routes.rb entries) to handle the auth flow.

require "koala"

def start
  redirect_to @oauth.url_for_oauth_code(:permissions=>"my permissions")

def callback
  session[:access_token] = @oauth.get_access_token(params[:code])
  redirect_to(:action=>"my action")

Does that make sense? You create a redirect uri, and then you push the user to the auth url (with the redirect_uri encoded in the querystring). On the return trip Facebook will send them to the redirect_uri. Then, you get a “code” in the querystring from FB, and the oauth object can evaluate that and grab the access token. Once you have that set- you can willy-nilly query the API object, as follows:

require ‘koala’

def some_method
  @api =[:access_token])
    @graph_data = @api.get_object("/me/photos")
  rescue Exception=>ex
    puts ex.message

  @graph_data do |photo|
    puts "my photo: #{}"

Voila! There are many more methods than “get_object”- and far more friendly. It’s just the most closely aligned with the Facebook Graph API.


  1. Comment by Cory Dolphin

    Posted on January 22, 2012 at 4:53 am

    Is there any chance of a more detailed writeup? Or a link to an example rails application that gets an OAuth token and uses it to call the Graph API in a workflow that is somewhat expected? I have yet to find one on the internet, and as a brand new rails developer, one would be very helpful.

  2. Comment by banane

    Posted on January 22, 2012 at 9:27 pm

    Sure- and other folks have asked for a project file dump. I'll do both.

  3. Pingback by banane » Blog Archive » Sample Koala-Facebook Ruby on Rails App

    Posted on January 22, 2012 at 10:47 pm

    […] on my old post: “Ruby on Rails and Facebook API (Koala): Basic Example” there was a request for either sample code, or more detail. I’ve setup an app in Github: […]

  4. Comment by banane

    Posted on January 23, 2012 at 6:30 am

    OK Cory, it's on Github now

  5. Comment by banane

    Posted on January 23, 2012 at 6:33 am

    Sure- sorry for the lateness in getting back to you! Here it is on github

  6. Comment by sonu kumar

    Posted on July 31, 2012 at 7:14 am

    Hi, i'm trying to create a iframe fb app. This auth method works fine but my application goes out of iframe and doesn't open in the fb. If i provide redirect_uri to my canvas page url , it failed to retrieve ':code' param. Any help would be great. :(

  7. Comment by banane

    Posted on July 31, 2012 at 1:06 pm

    I know how to do iFrame apps with PHP, but not Rails, not sure if that helps. The way you manage that is that you send it the same way to authenticate, but in the HREF to auth, remember to type “_top” to access the authentication flow through Facebook (not in the frame). When Facebook sends the user back to your app, you will be on the main index page. Sniff for the signed_request post object, and that will have a UID, noting that the user has authenticated. Koala has a flow for this, I just didn't do an example for that.

  8. Comment by Pandurengaraj Balraj

    Posted on November 27, 2012 at 9:59 am


    I am trying to make the session to nil and making the facebook to login once again is there any way of repeated login as I need.

  9. Comment by Mustafa Şahin

    Posted on March 28, 2013 at 10:02 am

    Hi Anna,

    I wanted to thank you for this great post. I've learned a lot in here. :)

  10. Comment by Ram

    Posted on April 25, 2013 at 11:23 am

    It is a good article i found while search about koala in google ocean . and one query i have regarding Koala i.e How can i do invite friends uisng Koala gem in rails application.

  11. Comment by Narendra RE

    Posted on September 8, 2014 at 1:23 pm

    It is a good article.

  12. Comment by gwhosubex

    Posted on December 1, 2014 at 7:56 am

    I can't tell where you're supposed to put the code you give in the article.

  13. Comment by banane

    Posted on December 10, 2014 at 6:16 pm

    Wherever you want to implement a Facebook authenticantion, or a call to get some information from Facebook. If you tell me more about your app, I can be more specific.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>