Twitter is an amazing service for connecting people all over the world to talk about the latest celebrity gossip.
As the README suggests, you'll be building a command line application to interact with twitter. Twitters web API
Because Twitter makes money selling access to their API; every request must be authenticated at the application level. This means you'll need to register your application with twitter so you can get your api key and secret.
- Go to dev.twitter.com and sign in with your twitter account
- Click your twitter avatar in the top right corner and then select "my applications"
- Click "Create New App"
- Fill in the Application details section.
- P.s. Both the
Website
andCallback URL
fields need a public website address; but it doesn't have to be yours. I put http://www.google.com/.
- Accept the "Rules of the Road"
- Click "Create Your Application"
- Click "API Keys"
- Use
mv .env.example .env
to use Dotenv to manage your secret data and rely on .gitignore to keep it out of your github. - Fill in your API Key and API Secret in
.env
At this point, your application can access twitter and read public information! Hooray! Now your app can keep up on @justinbeiber!
Let's check out what the Beib has to say, shall we?
Before we go all cahrazy and start programming some ruby, we'll want to try it from the terminal.
To do this, we'll be using the statuses/user_timeline API. This page tells us everything about how to get a twitter users timeline from their API. Let's break it down!
- Resource URL - This is the location on the Internet we'll be requesting statuses from.
- Parameters - This is a bunch of optional (or required) data the service could
use. The only one we need is
screen_name
. - Example Request - An example of both the URL to use to make the request, and the data returned by it.
Now, let's grab that data!
- Go back to apps.twitter.com
- Select your application
- Click on the
Test Oauth
button. - Note the
Consumer key
andConsumer secret
fields already have your Applications API Token and API Secret. - Fill in
Request URI
withhttps://api.twitter.com/1.1/statuses/user_timeline.json
- Fill in
Request query
withscreen_name=justinbeiber
- Click
See OAuth Signature for this request
Phew! All this work just to get some Beiber tweets! There are two interesting parts of this page:
- The
Authorization header
- This HTTP Header identifies your application to the Twitter service. This is the literal text that is transmitted as part of an http request. - The
cURL command
- This is a command you can copy and paste into your terminal to see Justin Beibers tweets!
Go ahead! Copy and paste the contents of the cURL command
into your terminal
and run it!
What's all that text that flies by!? It's tweets encoded as JSON!
Since JSON is somewhat hard to read, let's use Ruby to parse this down!
First, make sure you've followed all of the app registration
instructions; especially moving .env.example
to
.env
and placing your api key and api secret inside of it.
Good? Great! Now, create a file called scratch.rb
in this projects root
directory, paste the contents into it, and run it!
require 'json'
require 'simple_oauth'
require 'excon'
require_relative 'lib/twitter_creds'
authorization_header = SimpleOAuth::Header.new("get",
"https://api.twitter.com/1.1/statuses/user_timeline.json",
{ :screen_name => "justinbeiber" },
{ :consumer_key => API_KEY,
:consumer_secret => API_SECRET })
response = Excon.send("get", "https://api.twitter.com/1.1/statuses/user_timeline.json", {
:query => { :screen_name => "justinbeiber" },
:headers => { "Authorization" => authorization_header.to_s }
})
beibs_tweets = JSON.parse(response.body)
puts beibs_tweets.first
Well, wouldya look at that?! It's a ruby hash of Justin Beibers latest tweet! Now, tweets have quite a bit of Metadata; so let's investigate!
beibs_tweets.first.keys
# See anything interesting? How bout "text"?
beibs_tweets.first["text"]
Congratulations! You've interacted with twitters API! Now pour yourself your beverage of choice, since we've still got a ways to go if we want to be able to publish DMs and tweets as ourselves.
In order to send tweets for a user, your application needs to act on that users behalf. This is where the access token and access token secret come in to play.
When you log in to an application with your facebook, google, or twitter account it generates an access token and secret for the requesting application; which the application can then use to act on your behalf.
We're not going to tackle logging in with OAuth right now; instead we're going to rely on twitter's kindness in that they let us generate the access token for the owner of an application. So:
- Visit and log in to apps.twitter.com
- Select your application
- Click the "Permissions"
- Select "Read, Write and Access direct messages"
- Click "Update settings"
- You may need to refresh the page if it doesn't reflect the changes.
- Click the "API Keys" tab
- Scroll down to "Your access token"
- You should see
Access token
andAccess token secret
fields. If not, click theCreate my access token
button. - You may need to refresh the page if it doesn't reflect the changes.
- You may need to verify your phone number of your twitter account if you haven't already. Twitter is being more picky.
- Plug your
access_token
andaccess_token_secret
into.env
OK! Let's move on!
Twitter has a great HTTP API for sending direct messages to other users; which is how all those "social media experts" automatically DM people who follow them.
Let's try to DM ourselves!
- Click the "Test OAuth" button after you've selected your app
- Scroll to Request Settings and select
POST
- Fill in
Request URI:
withhttps://api.twitter.com/1.1/direct_messages/new.json
- Fill in
Request query:
withscreen_name=<YOUR TWITTER SCREEN NAME>&text=I know kung fu!
- Click
See OAuth signature for this request
- Copy and paste the
cURL command
into a terminal - Check your DMs
- Gape in awe at your ability to impersonate yourself with a computer program
Woah! We can send DMs! We can also post tweets or perform any action that requires a user authentication context! WE COULD MAKE A SOCIAL MEDIA APP AND IPO! OH M... Ok. Sorry. I'll calm down now.
How bout we do it in ruby?
Again, ensure you've Allowed your application to post on your behalf
Then copy and paste the following into scratch.rb:
require 'json'
require 'simple_oauth'
require 'excon'
require_relative 'lib/twitter_creds'
authorization_header = SimpleOAuth::Header.new("post",
"https://api.twitter.com/1.1/direct_messages/new.json",
{ :screen_name => "zspencer",
:text => "I can send myself DMs in Ruby!" },
{ :consumer_key => API_KEY,
:consumer_secret => API_SECRET,
:token => ACCESS_TOKEN,
:token_secret => ACCESS_TOKEN_SECRET })
response = Excon.send("post", "https://api.twitter.com/1.1/direct_messages/new.json", {
:query => { :screen_name => "zspencer",
:text => "I can send myself DMs in Ruby!" },
:headers => { "Authorization" => authorization_header.to_s }
})
puts response.body
puts response.status
A couple things to notice:
- You should update the
screen_name
keys in both hashes to be your actual screen name. - A hash representing the query variables is
duplicated in both
Excon.send
andSimpleOAuth::Header.new
. This is due to how OAuth works, and could be extracted to a variable.
But wow! Look at that! We can send API requests in ruby!
We've provided a more detailed breakdown of the motivation and inner workings of oauth for the obsessive learners in the cohort. It may cause more confusion than it clears up; but if you really want to know...