Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

jhjguxin/tqq2

Repository files navigation

Tqq2

> 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.

Waring

**Notice: It’s only work for ‘BBtang.com`, if you want to use it, may be you must fix many bug.**

Installation

gem install Tqq2

Usage Examples

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

Tqq2::Config.api_key = "1234567890"
Tqq2::Config.api_secret = "somethinglooksstrageandhardtoremember"
Tqq2::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 Tqq2.

1.The Authorization Code strategy with response_type set to code

# get authorize_url
client = Tqq2::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
# Tqq2::Client.from_code is a shortcut for client.auth_code.get_token("authorization_code_value")
client = Tqq2::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 = Tqq2::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.站内应用开发指南

# get token using signed_request
client = Tqq2::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}

6.Send message

def tqq_status_update(auth = nil, message = nil)
  if auth.present? and auth.provider.eql? "tqq"
    client = Tqq2::Client.from_hash(access_token: auth.access_token)
    message ||= "it's a test message ... by francis, from #{root_url}"
    #message ||= "it's a test message ... by francis, from #{app.root_url(host: "bbtang.com")}"
    clientip = Setting.server_ip
    clientip ||= "xxx.xxx.xxx.xxx"
    client.t.add(message, {clientip: Setting.server_ip, content: message, "openid" => auth.uid})
  end
end

API

You can find them in /lib/Tqq2/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.

About

A wrapper for 'open.t.qq.com' OAuth2 API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages