Class: Wechat::AuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/auth_client.rb,
lib/wechat/auth_client/version.rb

Defined Under Namespace

Classes: AccessToken

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appid, secret) ⇒ AuthClient

Returns a new instance of AuthClient.



19
20
21
# File 'lib/wechat/auth_client.rb', line 19

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

Instance Attribute Details

#appidObject (readonly)

Returns the value of attribute appid.



17
18
19
# File 'lib/wechat/auth_client.rb', line 17

def appid
  @appid
end

#secretObject (readonly)

Returns the value of attribute secret.



17
18
19
# File 'lib/wechat/auth_client.rb', line 17

def secret
  @secret
end

Instance Method Details

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



23
24
25
26
# File 'lib/wechat/auth_client.rb', line 23

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



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

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

#get_token(code) ⇒ Object



28
29
30
31
# File 'lib/wechat/auth_client.rb', line 28

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