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')


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_idString

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

Returns:

  • (String)


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

def client_id
  @client_id
end

#client_secretString

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

Returns:

  • (String)


18
19
20
# File 'lib/baidu/oauth/client.rb', line 18

def client_secret
  @client_secret
end

Instance Method Details

#authorization_code_flowFlow::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_flowFlow::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_flowFlow::Device

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

Returns:



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_flowFlow::ImplicitGrant

采用 Implicit Grant 方式获取 Access Token 的授权验证流程

Returns:



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

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:



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对应的授权相关信息, 包括授权对象(应用)、授权用户、授权的权限、授权时间,过期时间。

Examples:

返回的原始 JSON

{
    "client_id": "ZLycGmiUcCkrSb3t7zSD8uV6",
    "userid": 689911016,
    "scope": "basic super_msg",
    "create_time": 1364555477,
    "expire_in": 2591980
}

:client_id   Access Token对应应用的Api Key
:userid      授权用户的唯一id。如果Access Token是通过Client Credentials授权方式 获取的,则该字段值为0
:scope       Access Token最终的访问范围,即用户实际授予的权限列表(用户在授权页面时,有可能会取消掉某些请求的权限)
:create_time Access Token的生成时间(Unix时间戳),以秒为单位
:expires_in  Access Token剩余的有效时间,以秒为单位

Parameters:

  • access_token (String)

    授权之后应用得到的Access Token

Returns:

  • (Hash)

    如上描述

  • (nil)

    若参数中传递的 Access Token 已经过期或者无效,则返回 nil

See Also:



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