Class: SimpleWx::OAuth

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_wx/o_auth.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#error, #raise_flag

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OAuth

Returns a new instance of OAuth.



35
36
37
# File 'lib/simple_wx/o_auth.rb', line 35

def initialize options
  @code = options[:code]
end

Instance Attribute Details

#access_tokenObject

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

#codeObject (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_inObject (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

#openidObject

Returns the value of attribute openid.



33
34
35
# File 'lib/simple_wx/o_auth.rb', line 33

def openid
  @openid
end

#refresh_tokenObject

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

#resultObject (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

#scopeObject (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

#unionidObject (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



62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_wx/o_auth.rb', line 62

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

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/simple_wx/o_auth.rb', line 49

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_tokenObject



39
40
41
42
43
# File 'lib/simple_wx/o_auth.rb', line 39

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_infoObject



45
46
47
# File 'lib/simple_wx/o_auth.rb', line 45

def 
  UserInfo.get_auth_info(o_auth: self)
end