This commit is contained in:
guange 2016-08-13 16:24:37 +08:00
parent c9d32ca69b
commit 817fdad21d
2 changed files with 13 additions and 25 deletions

View File

@ -132,11 +132,11 @@ module Wechat
grant_type: 'refresh_token', grant_type: 'refresh_token',
refresh_token: user_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 end
def web_userinfo(web_access_token, openid, lang = 'zh_CN') 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
end end

View File

@ -2,31 +2,19 @@ require 'wechat/token/access_token_base'
module Wechat module Wechat
module Token module Token
class AuthAccessToken class AuthAccessToken < AccessTokenBase
def refresh # def refresh
data = client.get('token', params: { grant_type: 'client_credential', appid: appid, secret: secret }) # params = {
write_token_to_file(data) # appid: access_token.appid,
read_token_from_file # grant_type: 'refresh_token',
end # 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 end
end end