Class: Baidu::OAuth::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Support::Request

#get, #post

Constructor Details

#initialize(client_id = Baidu.client_id, client_secret = Baidu.client_secret) ⇒ Client

创建一个 OAuth API 实例

Examples:

如果不使用全局配置,则可以在创建新实例时指定 client_idclient_secret

client = Baidu::OAuth::Client.new('a_client_id', 'a_client_secret')

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

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_idString

申请创建应用后获取的 API Key

Returns:

  • (String)

13
14
15
# File 'lib/baidu/oauth/client.rb', line 13

def client_id
  @client_id
end

#client_secretString

申请创建应用后获取的 Secret Key

Returns:

  • (String)

16
17
18
# File 'lib/baidu/oauth/client.rb', line 16

def client_secret
  @client_secret
end

Instance Method Details

#code_flowFlow::Code

采用 Authorization Code 获取 Access Token 的授权验证流程

Returns:


38
39
40
41
42
# File 'lib/baidu/oauth/client.rb', line 38

[:code, :device].each do |flow|
  define_method("#{flow}_flow".to_sym) do
    Baidu::OAuth::Flow.const_get(flow.capitalize).new self
  end
end

#device_flowFlow::Device

采用 Device Code 获取 Access Token 的授权验证流程

Returns:


38
39
40
41
42
# File 'lib/baidu/oauth/client.rb', line 38

[:code, :device].each do |flow|
  define_method("#{flow}_flow".to_sym) do
    Baidu::OAuth::Flow.const_get(flow.capitalize).new self
  end
end

#refresh(token, params = {}) ⇒ Baidu::Session

刷新 Access Token

Parameters:

  • token (String)

    用于刷新 Access Token 用的 Refresh Token

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :scope (String)

    以空格分隔的权限列表,若不传递此参数,代表请求的数据访问操作权限与上次获取 Access Token 时一致。通过 Refresh Token 刷新 Access Token 时所要求的scope权限范围必须小于等于上次获取 Access Token 时授予的权限范围。关于权限的具体信息请参考“权限列表

Returns:

See Also:


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/baidu/oauth/client.rb', line 53

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