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

Methods inherited from Base

method_missing

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 options
  @code = options[:code]
  @access_token = options[:access_token]
  @openid = options[:openid]
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



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

Returns:

  • (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_tokenObject



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_infoObject



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

def 
  if @scope == "snsapi_base"
    UserInfo.get_base_info(openid: @openid)
  else
    UserInfo.get_auth_info(o_auth: self)
  end
end