class Qihu::Auth

Attributes

access_token[R]
expires_at[R]
expires_in[R]
oauth2[R]
redirect_uri[RW]
refresh_token[R]
token[RW]

Public Class Methods

new(client_id, client_secret, token={}, redirect_uri='oob', site='https://openapi.360.cn') click to toggle source
# 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

Public Instance Methods

authorize_url(options={}) click to toggle source
# File lib/qihu/auth.rb, line 24
def authorize_url(options={})
  @redirect_uri = options[:redirect_uri] if options[:redirect_uri]
  scope = options[:scope] ? options[:scope] : 'basic'
  display = options[:display] ? options[:display] : 'default'

  @oauth2.auth_code.authorize_url(:redirect_uri => @redirect_uri, :scope => scope, :display => display)
end
get_code_from_account(username, password, options={}) click to toggle source
# File lib/qihu/auth.rb, line 44
def get_code_from_account(username, password, options={})
  @redirect_uri = options[:redirect_uri] if options[:redirect_uri]
  conn = Faraday.new(@oauth2.site)
  res = conn.post(@oauth2.authorize_url, {
    :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='') click to toggle source
# 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={}) click to toggle source
# File lib/qihu/auth.rb, line 59
def get_token_from_account(username, password, options={})
  self.get_token(self.get_code_from_account(username, password, options))
end
get_token_from_hash(token={}) click to toggle source
# File lib/qihu/auth.rb, line 63
def get_token_from_hash(token={})
  @token = OAuth2::AccessToken.from_hash(@oauth2, token)
  return self
end