Created
January 28, 2012 09:09
-
-
Save fanzeyi/1693608 to your computer and use it in GitHub Desktop.
Fanfou OAuth SDK Ruby Ver.
This file contains 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
# -*- coding: utf-8 -*- | |
require 'fanfou' | |
a = Fanfou::Fanfou::new('xxx', 'xxx') | |
a.get_authorize_url | |
a.get_access_token(a.request_token.token, a.request_token.secret, VERIFIER) # get from authorize url | |
a.fanfou_request '/statuses/home_timeline' | |
a.fanfou_request('/statuses/user_timeline', args = {:id => 'fanzeyi'}) | |
a.fanfou_request('/statuses/update', post_args = {:status => 'Hello'}) | |
puts a | |
This file contains 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
# -*- coding: utf-8 -*- | |
require 'json' | |
require 'oauth' | |
module Fanfou | |
class Fanfou | |
def _encode(arg) | |
a = "" | |
arg.each { |k,v| | |
a += "#{k}=#{v}&" | |
} | |
a[0..-2] | |
end | |
def initialize(consumer_key, consumer_secret, oauth_base_url = "http://fanfou.com", api_base_url = "http://api.fanfou.com") | |
@consumer_key = consumer_key | |
@consumer_secret = consumer_secret | |
@oauth_base_url = oauth_base_url | |
@api_base_url = api_base_url | |
end | |
def get_authorize_url() | |
@consumer = OAuth::Consumer.new( | |
@consumer_key, | |
@consumer_secret, | |
{ | |
:site => @oauth_base_url, | |
} | |
) | |
@request_token = @consumer.get_request_token | |
@request_token.authorize_url | |
end | |
def get_access_token(request_token, request_token_secret, verifier) | |
@consumer = OAuth::Consumer.new( | |
@consumer_key, | |
@consumer_secret, | |
{ | |
:site => @oauth_base_url, | |
} | |
) | |
@request_token = OAuth::RequestToken.new( | |
@consumer, | |
request_token, | |
request_token_secret | |
) | |
@access_token = @request_token.get_access_token(:oauth_verifier => verifier) | |
set_access(@access_token.token, @access_token.secret) | |
end | |
def set_access(access_token, access_token_secret) | |
@api_client = OAuth::Consumer.new( | |
@consumer_key, | |
@consumer_secret, | |
{ | |
:site => @api_base_url, | |
} | |
) | |
@access_token = OAuth::AccessToken.new(@api_client, access_token, access_token_secret) | |
end | |
def fanfou_request(path, post_args = nil, args = {}) | |
path += ".json" | |
if not @access_token | |
raise 'No Access Token Set!' | |
end | |
method = if post_args then :post | |
else :get | |
end | |
if args | |
path += "?" + _encode(args) | |
end | |
if post_args == :get | |
result = JSON::parse(@access_token.get(path).body) | |
else | |
result = JSON::parse(@access_token.post(path, post_args).body) | |
end | |
result | |
end | |
def request_token | |
@request_token | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment