| 1 |
require 'oauthclient' |
| 2 |
|
| 3 |
# Get your own consumer token from http://twitter.com/apps |
| 4 |
consumer_key = 'EDIT HERE' |
| 5 |
consumer_secret = 'EDIT HERE' |
| 6 |
|
| 7 |
callback = ARGV.shift # can be nil for OAuth 1.0. (not 1.0a) |
| 8 |
request_token_url = 'https://api.twitter.com/oauth/request_token' |
| 9 |
oob_authorize_url = 'https://api.twitter.com/oauth/authorize' |
| 10 |
access_token_url = 'https://api.twitter.com/oauth/access_token' |
| 11 |
|
| 12 |
STDOUT.sync = true |
| 13 |
|
| 14 |
# create OAuth client. |
| 15 |
client = OAuthClient.new |
| 16 |
client.oauth_config.consumer_key = consumer_key |
| 17 |
client.oauth_config.consumer_secret = consumer_secret |
| 18 |
client.oauth_config.signature_method = 'HMAC-SHA1' |
| 19 |
client.oauth_config.http_method = :get # Twitter does not allow :post |
| 20 |
client.debug_dev = STDERR if $DEBUG |
| 21 |
|
| 22 |
# Get request token. |
| 23 |
res = client.get_request_token(request_token_url, callback) |
| 24 |
p res.status |
| 25 |
p res.oauth_params |
| 26 |
p res.content |
| 27 |
p client.oauth_config |
| 28 |
token = res.oauth_params['oauth_token'] |
| 29 |
secret = res.oauth_params['oauth_token_secret'] |
| 30 |
raise if token.nil? or secret.nil? |
| 31 |
|
| 32 |
# You need to confirm authorization out of band. |
| 33 |
puts |
| 34 |
puts "Go here and do confirm: #{oob_authorize_url}?oauth_token=#{token}" |
| 35 |
puts "Type oauth_verifier/PIN (if given) and hit [enter] to go" |
| 36 |
verifier = gets.chomp |
| 37 |
verifier = nil if verifier.empty? |
| 38 |
|
| 39 |
# Get access token. |
| 40 |
# FYI: You may need to re-construct OAuthClient instance here. |
| 41 |
# In normal web app flow, getting access token and getting request token |
| 42 |
# must be done in different HTTP requests. |
| 43 |
# client = OAuthClient.new |
| 44 |
# client.oauth_config.consumer_key = consumer_key |
| 45 |
# client.oauth_config.consumer_secret = consumer_secret |
| 46 |
# client.oauth_config.signature_method = 'HMAC-SHA1' |
| 47 |
# client.oauth_config.http_method = :get # Twitter does not allow :post |
| 48 |
res = client.get_access_token(access_token_url, token, secret, verifier) |
| 49 |
p res.status |
| 50 |
p res.oauth_params |
| 51 |
p res.content |
| 52 |
p client.oauth_config |
| 53 |
id = res.oauth_params['user_id'] |
| 54 |
|
| 55 |
puts |
| 56 |
puts "Access token usage example" |
| 57 |
puts "Hit [enter] to go" |
| 58 |
gets |
| 59 |
|
| 60 |
# Access to a protected resource. (DM) |
| 61 |
puts client.get("https://api.twitter.com/1.1/direct_messages.json") |