| 1 |
require 'oauthclient' |
| 2 |
require 'zlib' |
| 3 |
require 'stringio' |
| 4 |
|
| 5 |
# Get your own consumer token from http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html |
| 6 |
consumer_key = nil |
| 7 |
consumer_secret = nil |
| 8 |
|
| 9 |
callback = 'http://localhost/' # should point somewhere else... |
| 10 |
scope = 'https://www.googleapis.com/auth/buzz' |
| 11 |
request_token_url = 'https://www.google.com/accounts/OAuthGetRequestToken' |
| 12 |
access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken' |
| 13 |
|
| 14 |
STDOUT.sync = true |
| 15 |
|
| 16 |
# create OAuth client. |
| 17 |
client = OAuthClient.new |
| 18 |
client.oauth_config.consumer_key = consumer_key |
| 19 |
client.oauth_config.consumer_secret = consumer_secret |
| 20 |
client.oauth_config.signature_method = 'HMAC-SHA1' |
| 21 |
client.oauth_config.http_method = :get # Twitter does not allow :post |
| 22 |
client.debug_dev = STDERR if $DEBUG |
| 23 |
|
| 24 |
# Get request token. |
| 25 |
res = client.get_request_token(request_token_url, callback, :scope => scope) |
| 26 |
p res.status |
| 27 |
p res.oauth_params |
| 28 |
p res.content |
| 29 |
p client.oauth_config |
| 30 |
token = res.oauth_params['oauth_token'] |
| 31 |
secret = res.oauth_params['oauth_token_secret'] |
| 32 |
raise if token.nil? or secret.nil? |
| 33 |
|
| 34 |
# You need to confirm authorization out of band. |
| 35 |
puts |
| 36 |
puts "Go here and do confirm: https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?oauth_token=#{token}&domain=#{consumer_key}&scope=#{scope}" |
| 37 |
puts "Type oauth_verifier (if given) and hit [enter] to go" |
| 38 |
require 'cgi' |
| 39 |
verifier = CGI.unescape(gets.chomp) |
| 40 |
verifier = nil if verifier.empty? |
| 41 |
|
| 42 |
# Get access token. |
| 43 |
res = client.get_access_token(access_token_url, token, secret, verifier) |
| 44 |
p res.status |
| 45 |
p res.oauth_params |
| 46 |
p res.content |
| 47 |
p client.oauth_config |
| 48 |
id = res.oauth_params['user_id'] |
| 49 |
|
| 50 |
puts |
| 51 |
puts "Access token usage example" |
| 52 |
puts "Hit [enter] to go" |
| 53 |
gets |
| 54 |
|
| 55 |
# Access to a protected resource. |
| 56 |
# @consumption requires Buzz API |
| 57 |
puts client.get_content("https://www.googleapis.com/buzz/v1/activities/@me/@consumption", :alt => :json, :prettyprint => true) |