Last active
September 27, 2015 14:57
-
-
Save ashaw/1288064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TumblrOauth | |
class Client | |
def initialize(options = {}) | |
@consumer_key = options[:consumer_key] | |
@consumer_secret = options[:consumer_secret] | |
@token = options[:token] | |
@secret = options[:secret] | |
@proxy = options[:proxy] | |
end | |
def authorize(token, secret, options = {}) | |
request_token = OAuth::RequestToken.new( | |
consumer, token, secret | |
) | |
@access_token = request_token.get_access_token(options) | |
@token = @access_token.token | |
@secret = @access_token.secret | |
@access_token | |
end | |
def request_token(options={}) | |
consumer.get_request_token(options) | |
end | |
def authentication_request_token(options={}) | |
consumer.options[:authorize_path] = '/oauth/authorize' | |
request_token(options) | |
end | |
# API METHODS | |
def user_info(basename) | |
post("user/info") | |
end | |
### | |
private | |
def basename(username) | |
"#{basename}.tumblr.com" | |
end | |
def consumer | |
@consumer ||= OAuth::Consumer.new( | |
@consumer_key, | |
@consumer_secret, | |
{ :site => 'http://api.tumblr.com', :request_endpoint => @proxy } | |
) | |
end | |
def access_token | |
@access_token ||= OAuth::AccessToken.new(consumer, @token, @secret) | |
end | |
def get(path, headers={}) | |
headers.merge!("User-Agent" => "tumblr_oauth") | |
oauth_response = access_token.get("/api/v2/#{path}", headers) | |
JSON.parse(oauth_response.body) | |
end | |
def post(path, body='', headers={}) | |
headers.merge!("User-Agent" => "tumblr_oauth") | |
oauth_response = access_token.post("/api/v2/#{path}", body, headers) | |
JSON.parse(oauth_response.body) | |
end | |
def delete(path, headers={}) | |
headers.merge!("User-Agent" => "tumblr_oauth") | |
oauth_response = access_token.delete("/api/v2/#{path}", headers) | |
JSON.parse(oauth_response.body) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
client = TumblrOauth::Client.new( | |
:consumer_key => 'YOURKEY', | |
:consumer_secret => 'YOURSECRET' | |
) | |
request_token = client.request_token | |
puts "#{request_token.authorize_url}\n" | |
puts "Paste the 'oauth-verifier' from the query string on redirected page." | |
pin = STDIN.readline.chomp | |
access_token = client.authorize( | |
request_token.token, | |
request_token.secret, | |
:oauth_verifier => pin | |
) | |
puts access_token.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment