Created
June 17, 2011 13:10
-
-
Save marshluca/1031385 to your computer and use it in GitHub Desktop.
Tqq OAuth in Ruby
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
require "rubygems" | |
require "oauth" | |
TQQ_CONSUMER_KEY = "9b43a5afaca04afd8eb573e673fce3f9" | |
TQQ_CONSUMER_SECRET = "dd6b3cef9a6896e373085a83cd8b74f9" | |
# 1. 获取Request Token | |
consumer = OAuth::Consumer.new(TQQ_CONSUMER_KEY, TQQ_CONSUMER_SECRET, { | |
:site => "https://open.t.qq.com", | |
:request_token_path => "/cgi-bin/request_token", | |
:access_token_path => "/cgi-bin/access_token", | |
:authorize_path => "/cgi-bin/authorize", | |
:http_method => :get, | |
:scheme => :query_string, | |
:nonce => Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')[0, 32], # 32位随机字符串 | |
:realm => "http://www.5yi.com" | |
}) | |
callback_url = "http://localhost:3000/oauth/callback" | |
request_token = consumer.get_request_token(:oauth_callback => callback_url) | |
# 2. 用户确认授权 | |
url = "#{request_token.authorize_url}&oauth_token_secret=#{request_token.secret}&oauth_callback=#{callback_url}" | |
puts "请将下面url粘贴到浏览器中,并同意授权,同意后按任意键继续:\n#{url}" | |
sleep(30) # 将回调url里的oauth_verifier存到本地 | |
# 3. 换取Access Token | |
access_token = request_token.get_access_token({:oauth_verifier => IO.readlines("verifier.txt").first.strip}) | |
# 4. 访问受限资源 | |
response = access_token.get "http://open.t.qq.com/api/user/info?format=json" | |
puts response.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment