Class: Weibo::Oauth
- Inherits:
-
Object
- Object
- Weibo::Oauth
- Defined in:
- lib/weibo/oauth.rb
Constant Summary collapse
- AUTHORIZE_URL =
"https://api.weibo.com/oauth2/authorize"- ACCESS_TOKEN_URL =
"https://api.weibo.com/oauth2/access_token"- API_URL =
"https://api.weibo.com/2/"
Class Method Summary collapse
- .authorize_url(options = {}) ⇒ Object
- .get_access_token_by_code(code) ⇒ Object
- .parse_signed_request(signed_request) ⇒ Object
Instance Method Summary collapse
- #get(path, parameters = {}) ⇒ Object
-
#initialize(access_token) ⇒ Oauth
constructor
A new instance of Oauth.
- #post(path, parameters = {}) ⇒ Object
Constructor Details
#initialize(access_token) ⇒ Oauth
Returns a new instance of Oauth.
10 11 12 |
# File 'lib/weibo/oauth.rb', line 10 def initialize(access_token) @access_token = access_token end |
Class Method Details
.authorize_url(options = {}) ⇒ Object
22 23 24 25 |
# File 'lib/weibo/oauth.rb', line 22 def self.( = {}) url = AUTHORIZE_URL + "?client_id=#{Config.app_key}&redirect_uri=#{Config.redirect_uri}&response_type=code" .keys.length > 0 ? (url + "&" + .map {|k,v| "#{k}=#{v}"}.join("&")) : url end |
.get_access_token_by_code(code) ⇒ Object
27 28 29 30 31 |
# File 'lib/weibo/oauth.rb', line 27 def self.get_access_token_by_code(code) response = RestClient.post(ACCESS_TOKEN_URL, :client_id => Config.app_key, :client_secret => Config.app_secret, :grant_type => "authorization_code", :code => code, :redirect_uri => Config.redirect_uri) JSON.parse(response) end |
.parse_signed_request(signed_request) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/weibo/oauth.rb', line 38 def self.parse_signed_request(signed_request) encoded_sig, payload = signed_request.split(".") sig = Base64.decode64_url(encoded_sig) begin data = JSON.parse(Base64.decode64_url(payload)) rescue Exception => e return nil end return nil if data["algorithm"].upcase != "HMAC-SHA256" expected_sig = OpenSSL::HMAC.digest("sha256", Config.app_secret, payload) (sig != expected_sig) ? nil : data end |
Instance Method Details
#get(path, parameters = {}) ⇒ Object
14 15 16 |
# File 'lib/weibo/oauth.rb', line 14 def get(path, parameters = {}) JSON.parse RestClient.get(api_url(path), :params => parameters, :Authorization => "OAuth2 #{@access_token}") end |
#post(path, parameters = {}) ⇒ Object
18 19 20 |
# File 'lib/weibo/oauth.rb', line 18 def post(path, parameters = {}) JSON.parse RestClient.post(api_url(path), parameters, :Authorization => "OAuth2 #{@access_token}") end |