Class: Baidu::Client
- Inherits:
-
Object
- Object
- Baidu::Client
- Includes:
- Pcs, Tool, Translation, User
- Defined in:
- lib/baidu/client.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
OAuth2::AccessToken.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
Returns the value of attribute api_secret.
Instance Method Summary collapse
-
#authorize_url(opts = {}) ⇒ Object
OAuth2
authorize_url
. -
#initialize(&block) ⇒ Client
constructor
A new instance of Client.
-
#oauth_client ⇒ Object
OAuth2::Client instance with baido OAuth developer.baidu.com/wiki/index.php?title=docs/oauth/application.
-
#refresh_token!(refresh_token) ⇒ Object
OAuth refresh_token method.
-
#revert_token!(access_token) ⇒ Object
Revert #access_token info with String access_token.
- #token ⇒ Object
-
#token!(authorization_code, opts = {}) ⇒ Object
OAuth get_token method.
Constructor Details
#initialize(&block) ⇒ Client
Returns a new instance of Client.
20 21 22 |
# File 'lib/baidu/client.rb', line 20 def initialize(&block) instance_eval(&block) end |
Instance Attribute Details
#access_token ⇒ Object
OAuth2::AccessToken
Store authorized infos
18 19 20 |
# File 'lib/baidu/client.rb', line 18 def access_token @access_token end |
#api_key ⇒ Object
Returns the value of attribute api_key.
13 14 15 |
# File 'lib/baidu/client.rb', line 13 def api_key @api_key end |
#api_secret ⇒ Object
Returns the value of attribute api_secret.
13 14 15 |
# File 'lib/baidu/client.rb', line 13 def api_secret @api_secret end |
Instance Method Details
#authorize_url(opts = {}) ⇒ Object
OAuth2 authorize_url
redirect or open this URL for login in Baidu website
Params:
-
opts
Hash-
redirect_uri
String - default Baidu::DEFAULT_REDIRECT_URI,URL with logined redirect back
-
Rails example:
class SessionController
def oauth
redirect_to $baidu.(redirect_uri: callback_session_url)
end
def callback
auth_code = params[:code]
$baidu.token!(auth_code)
end
end
59 60 61 62 63 64 |
# File 'lib/baidu/client.rb', line 59 def (opts = {}) opts[:redirect_uri] ||= DEFAULT_REDIRECT_URI # scope:非必须参数,以空格分隔的权限列表,若不传递此参数,代表请求用户的默认权限。关于权限的具体信息请参考“权限列表”。 opts[:scope] ||= "basic netdisk super_msg" oauth_client.auth_code.(scope: opts[:scope], redirect_uri: opts[:redirect_uri]) end |
#oauth_client ⇒ Object
OAuth2::Client instance with baido OAuth developer.baidu.com/wiki/index.php?title=docs/oauth/application
27 28 29 30 31 32 33 |
# File 'lib/baidu/client.rb', line 27 def oauth_client @oauth_client ||= OAuth2::Client.new(api_key, api_secret, site: "https://openapi.baidu.com", authorize_url: "/oauth/2.0/authorize", token_url: "/oauth/2.0/token") end |
#refresh_token!(refresh_token) ⇒ Object
OAuth refresh_token method
Refresh tokens when token was expired
Params:
-
refresh_token - refresh_token in last got #access_token
96 97 98 99 |
# File 'lib/baidu/client.rb', line 96 def refresh_token!(refresh_token) old_token = OAuth2::AccessToken.new(oauth_client,'', refresh_token: refresh_token) self.access_token = old_token.refresh! end |
#revert_token!(access_token) ⇒ Object
Revert #access_token info with String access_token
You can store #access_token.token in you database or local file, when you restart you app, you can revert #access_token instance by that token
Params:
-
access_token - token in last got #access_token.token
110 111 112 |
# File 'lib/baidu/client.rb', line 110 def revert_token!(access_token) self.access_token = OAuth2::AccessToken.new(oauth_client, access_token) end |
#token ⇒ Object
83 84 85 |
# File 'lib/baidu/client.rb', line 83 def token @token ||= access_token.token end |
#token!(authorization_code, opts = {}) ⇒ Object
OAuth get_token method
This method will get #access_token (OAuth2::AccessToken) … and save in Baidu instance
Params:
-
authorization_code - Authorization Code in callback URL
-
opts
-
redirect_uri
String - default Baidu::DEFAULT_REDIRECT_URI,URL with logined redirect back
-
78 79 80 81 |
# File 'lib/baidu/client.rb', line 78 def token!(,opts = {}) opts[:redirect_uri] ||= DEFAULT_REDIRECT_URI self.access_token = oauth_client.auth_code.get_token(, redirect_uri: opts[:redirect_uri]) end |