[QUESTION] How to cache OAuth2::AccessToken?
Created by: aesyondu
I'm doing something like this:
token = Rails.cache.fetch(CACHE_KEY.to_s, expires_in: 1.hour) do
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET,
site: SITE,
token_url: OAUTH_ENDPOINT)
client.client_credentials.get_token
end
But it throws an error:
TypeError (no _dump_data is defined for class OpenSSL::X509::Store):
I'm only doing it because I want to reuse the access token and not request a new one for every request. Is there a better way to store the token?
Right now my workaround is to_hash
:
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET,
site: SITE,
token_url: OAUTH_ENDPOINT)
token_hash = Rails.cache.fetch(CACHE_KEY.to_s + 'abc', expires_in: 1.hour) do
client.client_credentials.get_token.to_hash
end
token = OAuth2::AccessToken.new client, token_hash
if token.expired?
token_hash = Rails.cache.fetch(CACHE_KEY.to_s, expires_in: 1.hour, force: true) do
client.client_credentials.get_token
end
OAuth2::AccessToken.new client, token_hash
else
token
end