Class: SimpleWx::OAuth
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#code ⇒ Object
readonly
Usage.
-
#expires_in ⇒ Object
readonly
Usage.
-
#openid ⇒ Object
Returns the value of attribute openid.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#result ⇒ Object
readonly
Usage.
-
#scope ⇒ Object
readonly
Usage.
-
#unionid ⇒ Object
readonly
Usage.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #access_token_valid? ⇒ Boolean
- #get_access_token ⇒ Object
- #get_user_info ⇒ Object
-
#initialize(options) ⇒ OAuth
constructor
A new instance of OAuth.
Methods inherited from Base
Constructor Details
#initialize(options) ⇒ OAuth
Returns a new instance of OAuth.
35 36 37 38 39 |
# File 'lib/simple_wx/o_auth.rb', line 35 def initialize @code = [:code] @access_token = [:access_token] @openid = [:openid] end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
33 34 35 |
# File 'lib/simple_wx/o_auth.rb', line 33 def access_token @access_token end |
#code ⇒ Object (readonly)
Usage
auth = SimpleWx::OAuth.new(code: “code”)
调用 OAth2 的 access_token 接口, 返回结果信息result_json = auth.get_access_token
auth 返回的基本参数会赋值给对象属性auth.access_token == result_json
> true
检查 access_token 是否过期auth.access_token_valid?
> true
auth.expires_in
> 7200
sleep 7201 auth.access_token_valid?
> false
刷新access_token接口result_json = auth.refresh_token auth.access_token == result_json auth.access_token_valid?
> true
32 33 34 |
# File 'lib/simple_wx/o_auth.rb', line 32 def code @code end |
#expires_in ⇒ Object (readonly)
Usage
auth = SimpleWx::OAuth.new(code: “code”)
调用 OAth2 的 access_token 接口, 返回结果信息result_json = auth.get_access_token
auth 返回的基本参数会赋值给对象属性auth.access_token == result_json
> true
检查 access_token 是否过期auth.access_token_valid?
> true
auth.expires_in
> 7200
sleep 7201 auth.access_token_valid?
> false
刷新access_token接口result_json = auth.refresh_token auth.access_token == result_json auth.access_token_valid?
> true
32 33 34 |
# File 'lib/simple_wx/o_auth.rb', line 32 def expires_in @expires_in end |
#openid ⇒ Object
Returns the value of attribute openid.
33 34 35 |
# File 'lib/simple_wx/o_auth.rb', line 33 def openid @openid end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
33 34 35 |
# File 'lib/simple_wx/o_auth.rb', line 33 def refresh_token @refresh_token end |
#result ⇒ Object (readonly)
Usage
auth = SimpleWx::OAuth.new(code: “code”)
调用 OAth2 的 access_token 接口, 返回结果信息result_json = auth.get_access_token
auth 返回的基本参数会赋值给对象属性auth.access_token == result_json
> true
检查 access_token 是否过期auth.access_token_valid?
> true
auth.expires_in
> 7200
sleep 7201 auth.access_token_valid?
> false
刷新access_token接口result_json = auth.refresh_token auth.access_token == result_json auth.access_token_valid?
> true
32 33 34 |
# File 'lib/simple_wx/o_auth.rb', line 32 def result @result end |
#scope ⇒ Object (readonly)
Usage
auth = SimpleWx::OAuth.new(code: “code”)
调用 OAth2 的 access_token 接口, 返回结果信息result_json = auth.get_access_token
auth 返回的基本参数会赋值给对象属性auth.access_token == result_json
> true
检查 access_token 是否过期auth.access_token_valid?
> true
auth.expires_in
> 7200
sleep 7201 auth.access_token_valid?
> false
刷新access_token接口result_json = auth.refresh_token auth.access_token == result_json auth.access_token_valid?
> true
32 33 34 |
# File 'lib/simple_wx/o_auth.rb', line 32 def scope @scope end |
#unionid ⇒ Object (readonly)
Usage
auth = SimpleWx::OAuth.new(code: “code”)
调用 OAth2 的 access_token 接口, 返回结果信息result_json = auth.get_access_token
auth 返回的基本参数会赋值给对象属性auth.access_token == result_json
> true
检查 access_token 是否过期auth.access_token_valid?
> true
auth.expires_in
> 7200
sleep 7201 auth.access_token_valid?
> false
刷新access_token接口result_json = auth.refresh_token auth.access_token == result_json auth.access_token_valid?
> true
32 33 34 |
# File 'lib/simple_wx/o_auth.rb', line 32 def unionid @unionid end |
Class Method Details
.generate_auth_link(url, state = "", scope = "snsapi_userinfo") ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/simple_wx/o_auth.rb', line 68 def self.generate_auth_link url, state = "", scope = "snsapi_userinfo" params = { appid: SimpleWx.weixin_config["app_id"], redirect_uri: url, response_type: "code", scope: scope, state: state } "https://open.weixin.qq.com/connect/oauth2/authorize?#{params.to_query}#wechat_redirect" end |
Instance Method Details
#access_token_valid? ⇒ Boolean
55 56 57 58 59 60 |
# File 'lib/simple_wx/o_auth.rb', line 55 def access_token_valid? url = "https://api.weixin.qq.com/sns/auth?access_token=#{@access_token}&openid=#{@openid}" response = RestClient.get url errcode_check(JSON.parse(response)) @error.blank? end |
#get_access_token ⇒ Object
41 42 43 44 45 |
# File 'lib/simple_wx/o_auth.rb', line 41 def get_access_token url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{SimpleWx.weixin_config["app_id"]}&secret=#{SimpleWx.weixin_config["app_secret"]}&code=#{@code}&grant_type=authorization_code" response = RestClient.get url set_result(errcode_check(JSON.parse(response))) end |
#get_user_info ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/simple_wx/o_auth.rb', line 47 def get_user_info if @scope == "snsapi_base" UserInfo.get_base_info(openid: @openid) else UserInfo.get_auth_info(o_auth: self) end end |