From 817fdad21d0b44fdba74134d4e984da2f9b12985 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Sat, 13 Aug 2016 16:24:37 +0800 Subject: [PATCH] . --- lib/wechat/lib/wechat/api.rb | 4 +-- .../lib/wechat/token/auth_access_token.rb | 34 ++++++------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/lib/wechat/lib/wechat/api.rb b/lib/wechat/lib/wechat/api.rb index 704af70b0..f267f8b69 100644 --- a/lib/wechat/lib/wechat/api.rb +++ b/lib/wechat/lib/wechat/api.rb @@ -132,11 +132,11 @@ module Wechat grant_type: 'refresh_token', refresh_token: user_refresh_token } - get 'oauth2/refresh_token', params: params, base: OAUTH2_BASE + get 'refresh_token', params: params, base: OAUTH2_BASE end def web_userinfo(web_access_token, openid, lang = 'zh_CN') - get 'userinfo', params: { access_token: web_access_token, openid: openid, lang: lang }, base: OAUTH2_BASE + get 'userinfo', params: { access_token: web_access_token, openid: openid, lang: lang }, base: OAUTH2_USERINFO end end diff --git a/lib/wechat/lib/wechat/token/auth_access_token.rb b/lib/wechat/lib/wechat/token/auth_access_token.rb index 811642dd7..5b58e261c 100644 --- a/lib/wechat/lib/wechat/token/auth_access_token.rb +++ b/lib/wechat/lib/wechat/token/auth_access_token.rb @@ -2,31 +2,19 @@ require 'wechat/token/access_token_base' module Wechat module Token - class AuthAccessToken + class AuthAccessToken < AccessTokenBase - def refresh - data = client.get('token', params: { grant_type: 'client_credential', appid: appid, secret: secret }) - write_token_to_file(data) - read_token_from_file - end + # def refresh + # params = { + # appid: access_token.appid, + # grant_type: 'refresh_token', + # refresh_token: user_refresh_token + # } + # data = client.get 'oauth2/refresh_token', params: params, base: OAUTH2_BASE + # write_token_to_file(data) + # read_token_from_file + # end - def read_token_from_file - td = JSON.parse(CacheFile.read(token_file)) - @got_token_at = td.fetch('got_token_at').to_i - @token_life_in_seconds = td.fetch('expires_in').to_i - @access_token = td.fetch('access_token') - rescue JSON::ParserError, Errno::ENOENT, KeyError - refresh - end - - def write_token_to_file(token_hash) - token_hash.merge!('got_token_at'.freeze => Time.now.to_i) - CacheFile.write(token_file, token_hash.to_json) - end - - def remain_life_seconds - token_life_in_seconds - (Time.now.to_i - got_token_at) - end end end end