parent
dde0fbb3c1
commit
70d58ccf85
@ -0,0 +1,33 @@
|
|||||||
|
require 'wechat/token/access_token_base'
|
||||||
|
|
||||||
|
module Wechat
|
||||||
|
module Token
|
||||||
|
class AuthAccessToken
|
||||||
|
|
||||||
|
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 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
|
||||||
|
|
Loading…
Reference in new issue