Quantcast
Channel: oditorium U » Tools
Viewing all articles
Browse latest Browse all 8

Authenticating against Twitter in Ruby

$
0
0

I sometimes use @sferik‘s twitter client t to do some command line stuff. I wanted to look at some network analysis, so I thought that t followers would be my friend, but alas no, I ran into rate limiting, probably because t goes away for each and everyone and gets details that I am not interested in. Having had a short twitter exchange with @sferik (thanks!) I learnt that t does not do that, but that the underlying Ruby library does. It took me a while to get it to work (the main issue being the authentication) so here a short cookbook style walkthrough in case you are interested.

Nothing that I am doing here is particularly original – in fact I mostly followed @sferik’s instructions and copied @sferik’s code from t where I was lacking information. So here we go.

What we want to run is the following:

require 'twitter'
puts 'Accesssing a  Twitter account'
# https://github.com/sferik/twitter/blob/master/examples/Configuration.md

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "*"
  config.consumer_secret = "*"
  config.access_token = "*"
  config.access_token_secret = "*"
end
client.update("I'm tweeting with @gem!")

but we are missing the following variables

config.consumer_key = "*"
config.consumer_secret = "*"
config.access_token = "*"
config.access_token_secret = "*"

The first two (consumer_key and consumer_secret) are easy to get: we need to go to apps.twitter.com and create a twitter app. Then the key and secret are under the API Keys tab. The problem is the second set, the access token and secret, which need to be obtained using Twitter’s three-step authentication. Code that does this (mostly from sferik’s library is as follows (obviously the lines key=... and secret=... must be filled beforehand):

require 'launchy'
require 'oauth'
require 'twitter'
require 'highline/import'

def generate_authorize_uri(consumer, request_token)
  request = consumer.create_signed_request(:get, consumer.authorize_path, request_token, pin_auth_parameters)
  params = request['Authorization'].sub(/^OAuth\s+/, '').split(/,\s+/).collect do |param|
    key, value = param.split('=')
    value =~ /"(.*?)"/
    "#{key}=#{CGI.escape(Regexp.last_match[1])}"
  end.join('&')
  "#{Twitter::REST::Client::ENDPOINT}#{request.path}?#{params}"
end

def pin_auth_parameters
  {:oauth_callback => 'oob'}
end

key = "-use-key-from-above-"
secret = "-use-key-from-above-"
puts "Your application API key is: "+key
puts "Your application API secret is: "+secret

consumer = OAuth::Consumer.new(key, secret, :site => Twitter::REST::Client::ENDPOINT)
request_token = consumer.get_request_token
uri = generate_authorize_uri(consumer, request_token)
puts "\nPlease navigate to the below URI in your browser:\n\n"+uri

pin = ask "\nOnce you have finished the authorisation procedure, please enter the PIN and hit Enter"
access_token = request_token.get_access_token(:oauth_verifier => pin.chomp)
oauth_response = access_token.get('/1.1/account/verify_credentials.json?skip_status=true')

puts "your Twitter id: "+oauth_response.body.id_str
puts "your screen name: "+oauth_response.body.screen_name
puts "your full name: "+oauth_response.body.name
puts "your access token:"+access_token.token
puts "your access token secret:"+access_token.secret

When running this script it prints out a URI. This URI should be pasted into browser that is already authenticated against Twitter. The page will display an authorise app button, and once this is pressed, Twitter will display a PIN. This PIN needs to be pasted into the script, which then will print the access token and access token secret. Done.

Note that if some of the necessary packages are not installed on the system one might need to run a subset of the following commands beforehand

gem install t
gem install twitter
gem install oauth
gem install highline

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images