获得 token
def self.get_qq_token(appkey, appsecret, redirect_uri, code)
url = URI('https://open.t.qq.com/cgi-bin/oauth2/access_token')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url.path+"?client_id=#{appkey}&client_secret=#{appsecret}&redirect_uri=#{redirect_uri}&grant_type=authorization_code&code=#{code}")
return http.request(request).body.to_s.split("&")[0].split("=")[1]
end
获得用户信息(读取操作)
def self.get_user_info(appkey, token, openid, ip)
url = URI("https://open.t.qq.com/api/user/info")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url.path+"?oauth_consumer_key=#{appkey}&access_token=#{token}&openid=#{openid}&clientip=#{ip}&oauth_version=2.a&scope=all")
return JSON.parse(http.request(request).body)["data"]
end
发布微博(写入操作)
def self.put(appkey, token, openid, ip, content)
url = URI("http://open.t.qq.com/api/t/add")
http = Net::HTTP.new(url.host, "443")
http.use_ssl = true
request = Net::HTTP::Post.new(url.path)
request.set_form_data({:oauth_consumer_key => appkey, :access_token => token, :openid => openid, :clientip => ip, :oauth_version => "2.a", :scope => "all", :content => content})
http.request(request).body
end
希望各位高手多多指点!