Class: QqClient::Oauth

Inherits:
Object
  • Object
show all
Defined in:
lib/qq_client/oauth.rb

Constant Summary collapse

API_URL =
"https://graph.qq.com/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Oauth

Returns a new instance of Oauth.



8
9
10
11
12
# File 'lib/qq_client/oauth.rb', line 8

def initialize(access_token)
  config = QqClient::Config::load_config(nil, nil, nil)
  @app_key = config[:app_key]
  @access_token = access_token
end

Class Method Details

.parse_signed_request(signed_request) ⇒ Object

Parameters:

  • string

    $signed_request 应用框架在加载iframe时会通过向Canvas URL post的参数signed_request



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qq_client/oauth.rb', line 35

def self.parse_signed_request(signed_request)
  encoded_sig, payload = signed_request.split(".")
  sig = Base64.decode64_url(encoded_sig)
  begin
    data = JSON.parse(Base64.decode64_url(payload))
  rescue Exception => e
    return nil
  end
  return nil if data["algorithm"].upcase != "HMAC-SHA256"

  expected_sig = OpenSSL::HMAC.digest("sha256", Config.app_secret, payload)
  (sig != expected_sig) ? nil : data
end

Instance Method Details

#get(uid, path, parameters = {}) ⇒ Object



14
15
16
17
# File 'lib/qq_client/oauth.rb', line 14

def get(uid, path, parameters = {})
  parameters.merge!(openid: uid,access_token: @access_token,oauth_consumer_key: @app_key)
  JSON.parse RestClient.get(api_url(path), :params => parameters)
end

#get_uid(path, parameters = {}) ⇒ Object



24
25
26
27
28
# File 'lib/qq_client/oauth.rb', line 24

def get_uid(path, parameters = {})
  parameters.merge!(access_token: @access_token,oauth_consumer_key: @app_key)
  callback_function = RestClient.get(get_uid_url(path, parameters))
  callback_function.match(/"openid":([^\/.]*)$/).to_a.second.split('"').second
end

#post(uid, path, parameters = {}) ⇒ Object



19
20
21
22
# File 'lib/qq_client/oauth.rb', line 19

def post(uid, path, parameters = {})
  parameters.merge!(openid: uid,access_token: @access_token,oauth_consumer_key: @app_key)
  JSON.parse RestClient.post(api_url(path), parameters)
end