Module: Wxapi::AccessToken
- Included in:
- Wxapi
- Defined in:
- lib/wxapi/access_token.rb
Instance Method Summary collapse
-
#get_access_token ⇒ string
获取 access_token 判断access_token_cache,决定是否需要缓存数据.
Instance Method Details
#get_access_token ⇒ string
获取 access_token 判断access_token_cache,决定是否需要缓存数据
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wxapi/access_token.rb', line 15 def get_access_token() appid = @app_id secret = @app_secret access_token_cache = @access_token_cache unless access_token_cache response = RestClient.get("#{prefix}/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}").body response_body = (JSON.parse response) # 抛出异常 throw response_body['errmsg'] unless response_body['access_token'] return response_body['access_token'] end _cache_key = "#{appid}_access_token" _cached_access_token = $redis.get _cache_key if _cached_access_token == nil or _cached_access_token == '' response = RestClient.get("#{prefix}/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}").body response_body = (JSON.parse response) # 抛出异常 throw response_body['errmsg'] unless response_body['access_token'] _cached_access_token = response_body['access_token'] $redis.set _cache_key, _cached_access_token, ex: 2.minutes end _cached_access_token end |