> base on weibo2, by francis
A Ruby wrapper for SA wrapper for ‘open.t.qq.com’ OAuth2 API.It is based on OAuth2 gem, 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.
**Notice: It’s only work for ‘BBtang.com`, if you want to use it, may be you must fix many bug.**
gem install ConnecConnectQQ
openid = JSON.parse client.account.get_openid.body.gsub(“callback( ”,“”).gsub(“ );n”,“”)
Config your api_key, api_secret and redrect_uri somewhere like development.rb.
ConnectQQ::Config.api_key = "1234567890" ConnectQQ::Config.api_secret = "somethinglooksstrageandhardtoremember" ConnectQQ::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 ConnectQQ.
1.The Authorization Code strategy with response_type set to code
# get authorize_url client = ConnectQQ::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 # ConnectQQ::Client.from_code is a shortcut for client.auth_code.get_token("authorization_code_value") client = ConnectQQ::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 = ConnectQQ::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.【QQ登录】OAuth2.0开发文档
# get token using signed_request client = ConnectQQ::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}
You can find them in /lib/ConnectQQ/interface/.Note that all methods follow the patten of
{resource}.{the_rest_path_joined_by_underline}(required_params, opts={})
So /statuses/hot/comments_weekly will turn to
statuses.hot_comments_weekly(opts={})
And /friendships/friends/in_common will turn to
friendships.friends_in_common(uid, opts={})
Copyright © 2011 Acenqiu. See LICENSE for details.