weibo2 icon indicating copy to clipboard operation
weibo2 copied to clipboard

A Ruby wrapper for Sina weibo API(OAuth2)

= Weibo2

A Ruby wrapper for Sina weibo API(OAuth2).It is based on {OAuth2 gem}[https://github.com/intridea/oauth2], thanks for his hard-working.I have wrapped most of the APIs sina defined.Note that all of the privilege APIs haven't been tested yet, since I don't get the authorization to use it.

== Installation

gem install weibo2

== Usage Examples

Config your api_key, api_secret and redrect_uri somewhere like development.rb.

Weibo2::Config.api_key = "1234567890" Weibo2::Config.api_secret = "somethinglooksstrageandhardtoremember" Weibo2::Config.redirect_uri = "http://www.example.com/callback"

Ok, now you are ready to enjoy it. Sina Weibo has provided several ways to get your access token, and you can easily get it using Weibo2.

1.The Authorization Code strategy with response_type set to code

get authorize_url

client = Weibo2::Client.new client.auth_code.authorize_url

=> "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=1234567890&redirect_uri=http%3A%2F%2Fwww.example.com%2fcallback"

get token using authorization_code

Weibo2::Client.from_code is a shortcut for client.auth_code.get_token("authorization_code_value")

client = Weibo2::Client.from_code("authorization_code_value")

2.The Authorization Code strategy with response_type set to token

get authorize_url with response_type set to token

client = Weibo::Client.new client.auth_code.authorize_url(:response_type => "token")

=> "https://api.weibo.com/oauth2/authorize?response_type=token&client_id=1234567890&redirect_uri=http%3A%2F%2Fwww.example.com%2fcallback"

get token from callback hash like this /callback#access_token=6874dd3766b35536abcb76a9e3a57867&expires_in=86400

client = Weibo2::Client.from_hash(:access_token => "6874dd3766b35536abcb76a9e3a57867", :expires_in => 86400)

3.The Resource Owner Password Credentials strategy

get token with user's password

client = Weibo::Client.new client.password.get_token('username', 'password')

4.Signed Request strategy

Follow this link to read more about this strategy.{站内应用开发指南}[http://open.weibo.com/wiki/%E7%AB%99%E5%86%85%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97]

get token using signed_request

client = Weibo2::Client.from_signed_request("signed_request-posted-by-weibo")

you can see what the signed_request exactly is by

client.signed_request.unsigned_request

=> {"user"=>{"country"=>"cn", "locale"=>""}, "algorithm"=>"HMAC-SHA256", "issued_at"=>1320298983, "expires"=>1320385383, "oauth_token"=>"0ca59d99f92436d65ae23115604a3185", "user_id"=>1234567890}

5.Refresh your token

Note that you could refresh your token only when you can get the refresh_token.

client.refresh!

You can check if you are authorized by

client.is_authorized?

=> true

If you are authorized, then you can do whatever you want now.

response = client.account.get_uid

=> #<OAuth2::Response:: ...>

response.parsed

=> {"uid"=>1234567890}

== API

You can find them in /lib/weibo2/interface/.Note that all methods follow the patten of

{resource}.{the_rest_path_joined_by_underline}(required_params, opts={})

So {/statuses/hot/comments_weekly}[http://open.weibo.com/wiki/2/statuses/hot/comments_weekly] will turn to

statuses.hot_comments_weekly(opts={})

And {/friendships/friends/in_common}[http://open.weibo.com/wiki/2/friendships/friends/in_common] will turn to

friendships.friends_in_common(uid, opts={})

== Copyright

Copyright (c) 2011 Acenqiu. See LICENSE for details.