logging in to a sinatra app with twitter and posting a tweet

Sat, Apr 6, 2013

I’ve been playing around with various integrations and decided to knock up a simple Twitter bunkup where you can login to a very simple Sinatra app using Twitter and send a tweet to say so. It’s very very simple as I wanted to concentrate on the Twitter integration rather than the app functionality. So how do you do it? First thing you need is a Sinatra app. I use rvm to manage dependencies so I like to create a .rvmrc file that loads the appropriate gemset whenever I cd into the directory:

The above will create a twitterauth directory, create a twitterauth gemset, install the required gems (note the high security on the twitter gem as it can do all sorts of things with your twitter account if you let it) and create an rvm .rvmrc file in the directory that will do the equivalent of rvm use 1.9.3@twitterauth every time you cd into it. The first time you cd in you’ll see gubbins about running the script for the first time and you have to choose to do so. After that it’s seamless and the gemset is loaded without any interaction.

The first time you cd into the directory after creating the .rvmrc file:

t1

Now we need to decide how we’re going to run this app. I ran it on my laptop which is called smeorach, which is Gaelic for a thrush btw. Essentially it’s going to run on localhost:4567 (the sinatra port) but twitter won’t let you register an app with a localhost domain so you need to fool it by using /etc/hosts. So sudo vi /etc/hosts and add this line ABOVE the line that looks like fe80::1%lo0:

127.0.0.1       smeorach.com

Although I’m using smeorach, twitter wil say http://smeorach:4567 is not a valid domain so I needed to use smeorach.com instead. It’s really localhost but we won’t tell twitter. Now head over to  https://dev.twitter.com/apps and register your sinatra app. I set my callback url to http://smeorach.com:4567/auth/twitter/callback. On the Settings tab choose Read and Write so your app can tweet on your behalf.

Once all that’s set up, create a new file called twitterauth.rb and include the required gems:

and initialise OmniAuth Twitter so we can login using Twitter:

and Twitter so we can tweet:

Now for some boilerplate:

/login is a Sinatra url while /auth/failure is provided by OmniAuth. The flow is summarised thusly:

t3

Point your browser at http://smeorach.com:4567/login, which manually redirects to the OmniAuth URL /auth/twitter which does the oauth flow and twitter redirects the browser back to http://smeorach.com:4567/auth/twitter/callback which is another OmniAuth URL. If OmniAuth detects an error it will redirect to /auth/failure for you. So what does /auth/twitter/callback look like?

The first line tweets the login event! It’s that simple, using the twitter gem. I’ve added the date and time to the tweet body otherwise every tweet after the first login will throw the error:

Twitter::Error::Forbidden - Status is a duplicate

The second line welcomes you to the app and displays your twitter avatar.

To use the app do:

ruby twitterauth.rb

and point your browser at http://smeorach.com:4567/login (or whatever your domain is) and login using twitter:

t2

If all goes well you’ll see a welcome message and your twitter avatar. Check your twitter timeline and you’ll see a new tweet too!

Tweeting from a sintra app

References:

comments powered by Disqus