Class: Qihu::Auth
- Inherits:
-
Object
- Object
- Qihu::Auth
- Defined in:
- lib/qihu/auth.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#oauth2 ⇒ Object
readonly
Returns the value of attribute oauth2.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #authorize_url(options = {}) ⇒ Object
- #get_code_from_account(username, password, options = {}) ⇒ Object
- #get_token(code, redirect_uri = '') ⇒ Object
- #get_token_from_account(username, password, options = {}) ⇒ Object
- #get_token_from_hash(token = {}) ⇒ Object
-
#initialize(client_id, client_secret, token = {}, redirect_uri = 'oob', site = 'https://openapi.360.cn') ⇒ Auth
constructor
A new instance of Auth.
Constructor Details
#initialize(client_id, client_secret, token = {}, redirect_uri = 'oob', site = 'https://openapi.360.cn') ⇒ Auth
Returns a new instance of Auth.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/qihu/auth.rb', line 10 def initialize(client_id, client_secret, token={}, redirect_uri='oob', site='https://openapi.360.cn') @redirect_uri = redirect_uri @oauth2 = OAuth2::Client.new(client_id, client_secret, :site => site, :authorize_url => '/oauth2/authorize', :token_url => '/oauth2/access_token', :ssl => {:verify => false}, ) if token or !token.empty? @token = OAuth2::AccessToken.from_hash(@oauth2, token) end end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
7 8 9 |
# File 'lib/qihu/auth.rb', line 7 def access_token @access_token end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
7 8 9 |
# File 'lib/qihu/auth.rb', line 7 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
7 8 9 |
# File 'lib/qihu/auth.rb', line 7 def expires_in @expires_in end |
#oauth2 ⇒ Object (readonly)
Returns the value of attribute oauth2.
7 8 9 |
# File 'lib/qihu/auth.rb', line 7 def oauth2 @oauth2 end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
8 9 10 |
# File 'lib/qihu/auth.rb', line 8 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
7 8 9 |
# File 'lib/qihu/auth.rb', line 7 def refresh_token @refresh_token end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/qihu/auth.rb', line 8 def token @token end |
Instance Method Details
#authorize_url(options = {}) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/qihu/auth.rb', line 24 def (={}) @redirect_uri = [:redirect_uri] if [:redirect_uri] scope = [:scope] ? [:scope] : 'basic' display = [:display] ? [:display] : 'default' @oauth2.auth_code.(:redirect_uri => @redirect_uri, :scope => scope, :display => display) end |
#get_code_from_account(username, password, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/qihu/auth.rb', line 44 def get_code_from_account(username, password, ={}) @redirect_uri = [:redirect_uri] if [:redirect_uri] conn = Faraday.new(@oauth2.site) res = conn.post(@oauth2., { :client_id => @oauth2.id, :redirect_uri => @redirect_uri, :response_type => 'code', :username => username, :password => password, }) query = CGI.parse(URI(res.headers[:location]).query) query["code"].pop end |
#get_token(code, redirect_uri = '') ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/qihu/auth.rb', line 32 def get_token(code, redirect_uri='') @redirect_uri = redirect_uri unless redirect_uri.empty? @token = @oauth2.auth_code.get_token(code, :redirect_uri => @redirect_uri) @refresh_token = @token.refresh_token @expires_at = @token.expires_at @expires_in = @token.expires_in @access_token = @token.token return self end |
#get_token_from_account(username, password, options = {}) ⇒ Object
59 60 61 |
# File 'lib/qihu/auth.rb', line 59 def get_token_from_account(username, password, ={}) self.get_token(self.get_code_from_account(username, password, )) end |
#get_token_from_hash(token = {}) ⇒ Object
63 64 65 66 |
# File 'lib/qihu/auth.rb', line 63 def get_token_from_hash(token={}) @token = OAuth2::AccessToken.from_hash(@oauth2, token) return self end |