Skip to content

Instantly share code, notes, and snippets.

@nov
Created December 15, 2010 05:26
Show Gist options
  • Save nov/741666 to your computer and use it in GitHub Desktop.
Save nov/741666 to your computer and use it in GitHub Desktop.
Hatena OAuth Sample in Ruby
require 'rubygems'
require 'oauth'
config = {
:client_key => SET_YOUR_OWN,
:client_secret => SET_YOUR_OWN,
:step => :temporary_credential
}
consumers = {
:oauth => OAuth::Consumer.new(
config[:client_key],
config[:client_secret],
{
:site => 'https://www.hatena.com',
:request_token_path => '/oauth/initiate',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/token'
}
),
:bookmark => OAuth::Consumer.new(
config[:client_key],
config[:client_secret],
{:site => 'http://b.hatena.ne.jp'}
)
}
case config[:step]
when :temporary_credential
request_token = consumers[:oauth].get_request_token({:oauth_callback => 'oob'}, {:scope => 'read_public'})
puts request_token.token, request_token.secret
puts request_token.authorize_url
when :token
request_token = {
:token => SET_YOUR_OWN,
:secret => SET_YOUR_OWN
}
verifier = SET_YOUR_OWN
request_token = OAuth::RequestToken.new(consumers[:oauth], request_token[:token], request_token[:secret])
access_token = request_token.get_access_token(:oauth_verifier => verifier)
puts access_token.token, access_token.secret
when :resource
consumer =
access_token = {
:token => SET_YOUR_OWN,
:secret => SET_YOUR_OWN
}
access_token = OAuth::AccessToken.new(consumers[:bookmark], access_token[:token], access_token[:secret])
res, data = access_token.get('/atom')
puts data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment