Using ruby & javascript for Facebook posts

Allowing auto-post to social media is pretty hot right now. Here’s how to do it via facebook. LinkedIn to follow.

  • Create koala.rb in your conifg/intializers directory

  • Our app doesn’t yet integrate oauth to our registration process yet. Rather we use facebook/twitter/linkedIn as auto-post accounts (think posterous). So we have a notification screen in which users can link their account with linkedin/twitter/facebook. I currently store the user’s facebook access ID, uid, and facebook user name. The access ID is needed for the post. The username is just used by our app for display only. So create a migration. (rails g migration AddFBToUsers fbaccess:string fbuser:string). See below for the migration. Remember to run rake db:migrate.

  • Ask the user to connect via facebook. FBConnect.js is run on load of page. Here, we check to see if the user has already authorized our facebook app. If so, we want to refresh their uid & access key (as access keys can expire over time).  If they authorize us (by clicking on fbconnect), we want to save that too. Doing this in javascript was the best way for us to control other page elements based on the user action.

  • We reference a post to savefbuid in the javascript. Make sure to add that to your routes. Below is method we use for save. You’ll see that I attempt to get the username. If that’s not available, I use the full name. New facebook users may not have a username yet. This is where we finally use Koala

  • Ok – we are ready to post. We use Koala for this too.



Good luck!

    Leave a Reply

    Powered by

    Up ↑

    %d bloggers like this: