Class: Baidu::OAuth::Client
- Inherits:
-
Object
- Object
- Baidu::OAuth::Client
- Includes:
- Support::Request
- Defined in:
- lib/baidu/oauth/client.rb
Constant Summary
Constants included from Support::Request
Support::Request::MAX_REDIRECT_LIMIT
Instance Attribute Summary collapse
-
#client_id ⇒ String
申请创建应用后获取的 API Key.
-
#client_secret ⇒ String
申请创建应用后获取的 Secret Key.
Instance Method Summary collapse
-
#authorization_code_flow ⇒ Flow::AuthorizationCode
采用 Authorization Code 获取 Access Token 的授权验证流程.
-
#client_credentials_flow ⇒ Flow::ClientCredentials
使用 Client Credentials 获取 Access Token 的授权验证流程.
-
#device_flow ⇒ Flow::Device
采用 Device Code 获取 Access Token 的授权验证流程.
-
#implicit_grant_flow ⇒ Flow::ImplicitGrant
采用 Implicit Grant 方式获取 Access Token 的授权验证流程.
-
#initialize(client_id = Baidu.client_id, client_secret = Baidu.client_secret) ⇒ Client
constructor
创建一个 OAuth API 实例.
-
#refresh(token, params = {}) ⇒ Baidu::Session
刷新 Access Token.
-
#token_info(access_token) ⇒ Hash?
查询Access Token对应的授权信息.
Methods included from Support::Request
Constructor Details
#initialize(client_id = Baidu.client_id, client_secret = Baidu.client_secret) ⇒ Client
创建一个 OAuth API 实例
26 27 28 29 30 |
# File 'lib/baidu/oauth/client.rb', line 26 def initialize(client_id=Baidu.client_id, client_secret=Baidu.client_secret) @client_id = client_id @client_secret = client_secret @site = Baidu::OAuth::SITE end |
Instance Attribute Details
#client_id ⇒ String
申请创建应用后获取的 API Key
15 16 17 |
# File 'lib/baidu/oauth/client.rb', line 15 def client_id @client_id end |
#client_secret ⇒ String
申请创建应用后获取的 Secret Key
18 19 20 |
# File 'lib/baidu/oauth/client.rb', line 18 def client_secret @client_secret end |
Instance Method Details
#authorization_code_flow ⇒ Flow::AuthorizationCode
采用 Authorization Code 获取 Access Token 的授权验证流程
52 53 54 55 56 57 |
# File 'lib/baidu/oauth/client.rb', line 52 [:authorization_code, :device, :implicit_grant, :client_credentials].each do |flow| define_method("#{flow}_flow".to_sym) do klass_name = flow.to_s.split('_').map { |s| s.capitalize }.join Baidu::OAuth::Flow.const_get(klass_name).new self end end |
#client_credentials_flow ⇒ Flow::ClientCredentials
使用 Client Credentials 获取 Access Token 的授权验证流程
52 53 54 55 56 57 |
# File 'lib/baidu/oauth/client.rb', line 52 [:authorization_code, :device, :implicit_grant, :client_credentials].each do |flow| define_method("#{flow}_flow".to_sym) do klass_name = flow.to_s.split('_').map { |s| s.capitalize }.join Baidu::OAuth::Flow.const_get(klass_name).new self end end |
#device_flow ⇒ Flow::Device
采用 Device Code 获取 Access Token 的授权验证流程
52 53 54 55 56 57 |
# File 'lib/baidu/oauth/client.rb', line 52 [:authorization_code, :device, :implicit_grant, :client_credentials].each do |flow| define_method("#{flow}_flow".to_sym) do klass_name = flow.to_s.split('_').map { |s| s.capitalize }.join Baidu::OAuth::Flow.const_get(klass_name).new self end end |
#implicit_grant_flow ⇒ Flow::ImplicitGrant
采用 Implicit Grant 方式获取 Access Token 的授权验证流程
52 53 54 55 56 57 |
# File 'lib/baidu/oauth/client.rb', line 52 [:authorization_code, :device, :implicit_grant, :client_credentials].each do |flow| define_method("#{flow}_flow".to_sym) do klass_name = flow.to_s.split('_').map { |s| s.capitalize }.join Baidu::OAuth::Flow.const_get(klass_name).new self end end |
#refresh(token, params = {}) ⇒ Baidu::Session
刷新 Access Token
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/baidu/oauth/client.rb', line 68 def refresh(token, params={}) body = { grant_type: 'refresh_token', refresh_token: token, client_id: self.client_id, client_secret: self.client_secret }.update params rest = post Baidu::OAuth::TOKEN_ENDPOINT, nil, body return nil if rest.nil? Baidu::Session.from rest end |
#token_info(access_token) ⇒ Hash?
查询Access Token对应的授权信息
该接口用于查询Access Token对应的授权相关信息, 包括授权对象(应用)、授权用户、授权的权限、授权时间,过期时间。
104 105 106 107 108 109 110 111 112 |
# File 'lib/baidu/oauth/client.rb', line 104 def token_info(access_token) body = { access_token: access_token } begin post Baidu::OAuth::TOKEN_INFO_ENDPOINT, nil, body rescue Baidu::Errors::ClientError => e return nil if e.code == 'invalid_grant' raise e end end |