class Baidu::Client
Attributes
OAuth2::AccessToken
Store authorized infos
Public Class Methods
# File lib/baidu/client.rb, line 20 def initialize(&block) instance_eval(&block) end
Public Instance Methods
OAuth2::Client instance with baido OAuth developer.baidu.com/wiki/index.php?title=docs/oauth/application
# 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
OAuth refresh_token method
Refresh tokens when token was expired
Params:¶ ↑
-
refresh_token - refresh_token in last got
access_token
# 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 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 gotaccess_token
.token
# File lib/baidu/client.rb, line 110 def revert_token!(access_token) self.access_token = OAuth2::AccessToken.new(oauth_client, access_token) end
# File lib/baidu/client.rb, line 83 def token @token ||= access_token.token end
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
-
# File lib/baidu/client.rb, line 78 def token!(authorization_code,opts = {}) opts[:redirect_uri] ||= DEFAULT_REDIRECT_URI self.access_token = oauth_client.auth_code.get_token(authorization_code, redirect_uri: opts[:redirect_uri]) end
Private Instance Methods
# File lib/baidu/client.rb, line 134 def get_response_json(api_url) JSON.parse(access_token.get(api_url).body) end
高级API列表
# File lib/baidu/client.rb, line 130 def query_params(params) params.merge({access_token: token}).to_query end