Posting to Facebook from a Ruby on Rails App

Once you get your head wrapped around OAuth and Facebook's specific implementation of it, this isn't too bad. That said, it took me a little while to get this figured out and there wasn't a good example of this specific use case, so here goes!

You should definitely checkout the Facebook Graph API Overview as it explains all the things you can do in detail. Here, I just connect with OAuth with the "offline_access" and "publish_stream" permissions which lets this application post things to Facebook without requiring any further input from the user or requiring them to be active on the site. (The site I wrote this for let's users schedule something to be posted, so it needs to be able to post at that time regardless of if the user is around or not.) You will need to have registered an application on Facebook to do this. The basic application flow:

  1. User visits facebook_account/new when they want to link in Facebook
  2. They get redirected to the allow/deny page on Facebook that prompts the user to log in and accept your application
  3. Assuming they accept, they get redirected back to callback/facebook/#{ID} with a "code" paramater (The oauth verification token)
  4. The application sends that code back to Facebook to retrieve an access_token, which can be used for all future posts to Facebook to act as the user

Assuming you're using Rails 3, everything below is stubbed out and may work out of the box, but you should use it as an example and write code yourself that does error handling, etc. The code in my actual project is a lot more comprehensive but I trimmed it out so that the basic flow here is easier to understand. Don't just copy/paste this all and expect it to work! No warranty is provided. The Sun may explode at any time.

First get the gems you'll need:

and run "bundle install".

Then set up some routes:

And create a table in a migration to hold the information. Run "rails generate migration CreateFacebookAccount" and put this in the file it creates for you:

The model takes care of building the authorize URL, validating the token we get back, and posting:

While the controller redirects when needed and handles the callback from facbeook:

And thats it! Anywhere else in your account you can call .post('Hello World') on an active FacebookAccount and it'll show up on the linked user's Facebook wall.

nice post, thanks for

nice post, thanks for sharing. looks like you're picking up the ruby_coding_conventions and patterns without a snag. seems fun. i'm jealous you get to work in ruby every day

as a side note, the gists don't show up in google reader for your feed... i had to click through to the article to read the actual code snippits. not a big deal, but figured i'd give a heads up, cause i'm not sure if you read your own posts in a feed reader ;P

Thanks! I do follow my RSS in

Thanks! I do follow my RSS in Google Reader and was sad when the gists didn't show up. I'll see if there is a way to do something that works better in the future.

Highgroove is always hiring ;)

Any reason to not integrate

Any reason to not integrate the FacebookAccount model with the existing User model?

If there was just one

If there was just one service, that would definitely make sense but when there are N of them it keeps things cleanly separated

Looks like an input format is

Looks like an input format is b0rked, because your stubbed code is gone.

It's all on Github. They go

It's all on Github. They go down sometimes.

Wonderful! I could use your

Wonderful! I could use your instructions to enhance my mobile dog website (german).
Thanks so much for sharing!!

Hi, I have tried to use your

Hi,
I have tried to use your code, however I am gettig an error:
uninitialized constant FacebookAccount::RestClient

Would you be able to provide a reason why this is happening and how I could fix it?

Thanks

Alexander

Make sure you have the

Make sure you have the rest-client Gem installed.

is there anyway in which one

is there anyway in which one can allow the user to have a check box/button like twitter does to connect to Facebook. so that the user can decide at any given time whether or not they want the comments/posts to be connected with the accounts.