tutorial, how to use twitter_auth gem in development mode
It was the big things weeks ago, but I didn’t had time to test it. Twitter_auth, allows you to user the twitter authentication on you application. And it’s really sweet!.
Let’s do a small implementation together.
Step 1: Prepare your development box
Because Twitter doesn’t allow the callback parameter anymore, you’ll need to setup your workstation.
I recommend to install passenger in your development box, and use the hosts file to manage your different applications.
For that, just follow the official passenger doc. Easy peasy.
Then you’ll need to prepare your localhost. I like to work with [applicationName].local.
sudo nano /etc/hosts
Then add an extra line
127.0.0.1 twitterauth.local
Now create your rails app
rails -d mysql twitterauth
or take my source code my github twitterauth-test
Finally add the site to apache
sudo nano /etc/apache2/sites-enabled/twitterauth
Then copy (and modify) these lines on it
<VirtualHost *:80> ServerName twitterauth.local DocumentRoot "/home/[REPLACE WITH YOU PATH]/twitterauth/public" RailsEnv development RailsAllowModRewrite off <directory "/home/[REPLACE WITH YOU PATH]/twitterauth/public"> Order allow,deny Allow from all </directory> </VirtualHost>
Restart apache
sudo /etc/init.d/apache2 restart
You now have your development box working with passenger. Next application you’ll develop, just add a new line in the /etc/hosts file, a new site-enabled file restart apache and you’re ready.
Step 2: Install twitter_auth as a depency
Modify you environement.rb by adding
config.gem 'twitter-auth', :lib => 'twitter_auth'
Then sudo rake gems:install, and you’ll have the gem installed.
Step 3: Prepare the application
script/generate twitter_auth rake db:create rake db:migrate touch tmp/restart.txt
In order to work properly, your app (even on development mode) need to be registered on twitter. Visit Twitter apps and form with your informations.
Twitter doesn’t accept the callback param anyone, so you’ll need to provide http://twitterauth.local/oauth_callback as a callback in the configuration form.
When you save the informations, Twitter will give you 2 keys that need to be copied in config/twitter_auth.yml.
then
touch tmp/restart.txt
Your app is now ready!
Step 4: Let’s do some code
We are going to create a twitter controller, with an index action
./script/generate controller Twitter index
Twitter_auth use the REST twitter api style, so to the loggued user friend time line copy and paste this in twitter_controller.rb, and returns a Json array. (visit the twitter api wiki for more informations)
Before calling functions that need you to be logged in, you’ll have to use the login_required method on before_filter.
This is going to be our root method so add in routes.rb
map.root :controller => "twitter"
This code goes into twitter_controller.rb
before_filter :login_required
#Retreive the friend timeline
def index
@tweets = current_user.twitter.get('/statuses/friends_timeline')
logger.info @tweets
end
Then in your views/twitter/index.html.rb
<ul class="tweets"> <% @tweets.each do |tweet| %> <li><%= link_to(tweet['user']['screen_name'] + ':', 'http://twitter.com/' + tweet['user']['screen_name'], :target => '_blank') + tweet['text'] %></li> <% end -%></ul>
After a bit of CSS, if you open http://twitterauth.local you should have something like this



Thanks for the tutorial.
when I tried restarting Apache, I got this error
Syntax error on line 4 of /etc/apache2/sites-enabled/twitterauth:
Invalid command ‘RailsEnv’, perhaps misspelled or defined by a module not included in the server configuration
Also, is the directory ’site-enabled’ or ’sites-enabled’?
Any suggestions?
Thank you
It worked!
I changed twitterauth.local, to http://localhost.com:3000/oauth_callback, for the callback URL, added 127.0.0.1 localhost localhost.com to the /etc/hosts file and it worked.
just a suggestion.
could you explain how to log off? also, do we really need the Apache step?
sorry for so many questions. rails noob here
OK, just clear these two cookies
_twitterauth_session and remember_token
Thanks for the tutorial
I like your syntax highlighter
Btw, a new version is out which automatically hides the toolbar, I think you might like that feature.
can twitter_auth be used ala openid?
Thanks ! It worked
Really nice tutorial
Damned, Wordpress didn’t send me emails about the comment guys, sorry for the late response!
So, @vijay for the logout, just create a link => link_to ‘Logout’,logout_path (checkout rake routes)
@Alex, OpenId store more information than twitter, but yes it kind does the same job. Thanks for the tip I’ll update soon.
@Hemali, you’re welcome
I finally got this to work. Hopefully, this will help at least one person that suffered the same way I did. LOL
First of all, after I registered my app with Twitter and made changes, the changes took a while to work. I KEPT getting 401 unauthorized for hours. Even though the code worked before! I simply changed the name of the app and it wasn’t ready. So keep that in mind. Don’t make changes until you have to!
Second, I had been playing with OpenID and AuthLogic. I had forgotten to take the “current_user” helper function out of the application controller so it was conflicting with the current_user used by TwitterAuth.
Now things work PERFECT!
Thanks for the tutorial.