Class: SimpleWechat::AuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_wechat/auth_client.rb

Defined Under Namespace

Classes: AccessToken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appid, secret) ⇒ AuthClient

Returns a new instance of AuthClient.



18
19
20
# File 'lib/simple_wechat/auth_client.rb', line 18

def initialize(appid, secret)
  @appid, @secret = appid, secret
end

Instance Attribute Details

#appidObject (readonly)

Returns the value of attribute appid.



16
17
18
# File 'lib/simple_wechat/auth_client.rb', line 16

def appid
  @appid
end

#secretObject (readonly)

Returns the value of attribute secret.



16
17
18
# File 'lib/simple_wechat/auth_client.rb', line 16

def secret
  @secret
end

Instance Method Details

#authorize_url(redirect_uri, state, options = {}) ⇒ Object



22
23
24
25
# File 'lib/simple_wechat/auth_client.rb', line 22

def authorize_url(redirect_uri, state, options = {})
  scope = options[:scope] ||= "snsapi_base"
  "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{appid}&redirect_uri=#{CGI.escape redirect_uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect" 
end

#connectionObject



32
33
34
35
36
37
38
# File 'lib/simple_wechat/auth_client.rb', line 32

def connection
  @connection ||= begin
    conn = Faraday.new do |faraday|
      faraday.adapter  Faraday.default_adapter
    end 
  end
end

#get_token(code) ⇒ Object



27
28
29
30
# File 'lib/simple_wechat/auth_client.rb', line 27

def get_token(code)
  response = connection.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{appid}&secret=#{secret}&code=#{code}&grant_type=authorization_code")
  access_token = AccessToken.new MultiJson.load(response.body)
end