Allowing auto-post to social media is pretty hot right now. Here’s how to do it via facebook. LinkedIn to follow.
- Get the koala gem. https://github.com/arsduo/koala/wiki/
- Get your facebook API keys. https://www.facebook.com/developers/createapp.php
- Create facebook.yml in your config directory
https://gist.github.com/983651
- Create koala.rb in your conifg/intializers directory
https://gist.github.com/983655
- 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.
https://gist.github.com/983665
- 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.
https://gist.github.com/983674
https://gist.github.com/983681
- 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
https://gist.github.com/983692
- Ok – we are ready to post. We use Koala for this too.
https://gist.github.com/983701
Good luck!
Leave a Reply