We all want shortcuts.
As of the end of this month, Twitter will deprecate support for http authentication (provide username and password explicitly) in API request. The alternative is called OAuth and Twitter has a comprehensive guide here. However, it appears too complex to us, and what we need is a method as similar to http auth as possible, since for researchers like us, the sole purpose of using API request is to get data via a robot account.
Here is the simplest shortcut, illustrated in Ruby.
- Register an app at
http://dev.twitter.com/apps/new, and you’ll get a consumer key and a consumer secret. From the page “my access token”, you can find an access token and an access secret. - Write the four strings into a config file, e.g.
$HOME/.twitter. Below is a yaml config example.#!/usr/bin/ruby -w # require 'pp' require 'yaml' CToken = "AAAAAAAAAAAAAAAAAAAAA" CSecret = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" AToken = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" ASecret = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD" config = {"ctoken" => CToken, "csecret"=>CSecret, "atoken"=>AToken, "asecret"=>ASecret} File.open("#{ENV["HOME"]}/.twitter", "w") do |f| f.puts config.to_yaml end
- Authorize your application in a script, using twitter gem.
#!/usr/bin/ruby -w require 'pp' require 'yaml' require 'twitter' require 'mysql' conf = {} File.open("#{ENV["HOME"]}/.twitter", "r") { |f| conf = YAML.load(f) } # twitter authentication oauth = Twitter::OAuth.new(conf["ctoken"], conf["csecret"]) oauth.authorize_from_access(conf["atoken"], conf["asecret"]) client = Twitter::Base.new(oauth) client.home_timeline.each { |tweet| pp tweet } exit

