目前的做法是
# encoding : utf-8 -*-
class TaobaoAppToken < ActiveRecord::Base
attr_accessible :access_token, :refresh_token, :last_refresh_at, :refresh_token_last_refresh_at
def check_or_refresh!
refresh_token_refresh = Time.now - self.refresh_token_last_refresh_at
if refresh_token_refresh > 3600 #1 hours
base_url = "https://oauth.taobao.com/token?"
params = {
client_id: Setting.taobao_app_key,
client_secret: Setting.taobao_app_secret,
grant_type: 'refresh_token',
refresh_token: self.refresh_token
}.to_params
response = HTTParty.post(base_url + params).parsed_response
if response['access_token'].present?
self.update_attributes(access_token: response['access_token'], last_refresh_at: Time.now, refresh_token: response['refresh_token'], refresh_token_last_refresh_at: Time.now )
else
Notifier.app_token_errors(self,response).deliver
end
end
end
end
每天会请求 12 次刷新,每次刷新都能获取到新的 acess_token 和 refresh_token, 但是认证一天后还是会提示 refresh_token 过期