Integrating Facebook into Rails

Recently, I integrated facebook’s new Graph API into Noteleaf. Though it’s far far easier than the old API, which almost didn’t warrant a blog post. However, authorization took a little bit of figuring out, so I thought I’d share.

If you’re using the languages blessed by Facebook, such as PHP, Javascript, Python, Objective-C and Java Android, then there’s already an SDK for you.

However, as a rubyist, we were on our own. Since we’re using Authlogic, we should be able to find a plugin for authenticating facebook.

On the Authlogic docs, it has authlogic_facebook_connect listed. It also depends on the most popular ruby gem for facebook integration, facebooker. However, facebooker’s documentation is shabby and the tutorials are out of date. I didn’t want to be digging around in something that wasn’t our core value proposition. So I didn’t end up going that route.

I was digging for alternatives, but in all the wrong places. It wasn’t until I was reading the facebook api docs more carefully, that I realized I should be looking for an OAuth2 module for Authlogic. After that, it was a breeze.

The instructions for authlogic_oauth2 are pretty clear, but here’s some tips. Beyond the instructions in authlogic_oauth2, make sure you set the oauth2_scope to request offline_access. If you don’t, when the user’s facebook session expires, your oauth_token that you stored in the user’s database will be expired. That means that after a while, the user won’t be able to log back in without requesting another token.

class UserSession < Authlogic::Session::Base
  oauth2_scope          "offline_access,email"
end

And if you do store the user’s facebook id locally, make sure it’s a big int.

class AddFacebookIdToUser < ActiveRecord::Migration
  def self.up
    add_column :users, :facebook_id, :bigint, :limit => 8
  end

  def self.down
    remove_column :users, :facebook_id
  end
end

which you can subsequently set in a before_create filter in User model.

class User < ActiveRecord::Base
  before_create :populate_oauth2_user

  private
  def populate_oauth2_user
    return if oauth2_token.blank?

    response = oauth2_access.get('/me')
    user_data = JSON.parse(response)
    if !user_data['id'].blank?
      self.facebook_id = user_data['id']
    end
  end
end

You may also want to consider using the provided javascript SDK. That way, you’d be able to load your page first, and have the client’s browser request the rest of the facebook data, so it appears you page loads faster.

Advertisements

4 thoughts on “Integrating Facebook into Rails

  1. Hi, thanks for the tips! You mentioned that “.. That means that after a while, the user won’t be able to log back in without requesting another token.”. I wonder, in case I choose not to have the “Offline Access” permission, how should I explicitly request for another token without any action from users? Many thanks in advance!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s