Class: Wechat::Core::AccessToken

Inherits:
Object
  • Object
show all
Extended by:
Common
Defined in:
lib/wechat/core/access_token.rb

Constant Summary

Constants included from Common

Common::ERROR_CODES, Common::LANGUAGE_ENGLISH, Common::LANGUAGE_SIMPLIFIED_CHINESE, Common::LANGUAGE_TRANDITIONAL_CHINESE

Class Method Summary collapse

Methods included from Common

assert_present!, get_json, post_json

Class Method Details

.create(app_id, app_secret) ⇒ Object

获取 Access Token mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html

Return hash format if success:

access_token: <ACCESS_TOKEN>,
expires_in: 7200

The ACCESS_TOKEN is 107 characters in 2015.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wechat/core/access_token.rb', line 35

def self.create(app_id, app_secret)

  assert_present! :app_id,     app_id
  assert_present! :app_secret, app_secret

  #message = ::JSONClient.new.get 'https://api.weixin.qq.com/cgi-bin/token',
  #  {
  #    grant_type: 'client_credential',
  #    appid:      app_id,     # Rails.application.secrets.wechat_app_id,
  #    secret:     app_secret, # Rails.application.secrets.wechat_app_secret
  #  }
  message = get_json 'https://api.weixin.qq.com/cgi-bin/token', body:
    {
      grant_type: 'client_credential',
      appid:      app_id,     # Rails.application.secrets.wechat_app_id,
      secret:     app_secret, # Rails.application.secrets.wechat_app_secret
    }
  message.body
end

.load(app_id, app_secret) ⇒ Object

获取 Access Token mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html

Return hash format if success:

access_token: <ACCESS_TOKEN>,
expires_in: 7200

The ACCESS_TOKEN is 107 characters in 2015.



16
17
18
19
20
21
22
23
24
# File 'lib/wechat/core/access_token.rb', line 16

def self.load(app_id, app_secret)
  message = ::JSONClient.new.get 'https://api.weixin.qq.com/cgi-bin/token',
    {
      grant_type: 'client_credential',
      appid:      app_id,     # Rails.application.secrets.wechat_app_id,
      secret:     app_secret, # Rails.application.secrets.wechat_app_secret
    }
  message.body
end