[deviantart] use cache to store new refresh_tokens
The 'refresh_token' set in a user's config file gets used once to get a new 'access_token' and 'refresh_token', which is then stored in gallery-dl's cache and gets used the next time the 'access_token' needs to be refreshed. This means deleting the cache file invalidates the refresh_token- chain and requires the user to re-authenticate.
This commit is contained in:
@@ -438,15 +438,21 @@ class DeviantartAPI():
|
|||||||
if refresh_token:
|
if refresh_token:
|
||||||
self.log.info("Refreshing access token")
|
self.log.info("Refreshing access token")
|
||||||
data = {"grant_type": "refresh_token",
|
data = {"grant_type": "refresh_token",
|
||||||
"refresh_token": refresh_token}
|
"refresh_token": _refresh_token_cache(refresh_token)}
|
||||||
else:
|
else:
|
||||||
self.log.info("Requesting public access token")
|
self.log.info("Requesting public access token")
|
||||||
data = {"grant_type": "client_credentials"}
|
data = {"grant_type": "client_credentials"}
|
||||||
|
|
||||||
auth = (self.client_id, self.client_secret)
|
auth = (self.client_id, self.client_secret)
|
||||||
response = self.session.post(url, data=data, auth=auth)
|
response = self.session.post(url, data=data, auth=auth)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
raise exception.AuthenticationError()
|
raise exception.AuthenticationError()
|
||||||
return "Bearer " + response.json()["access_token"]
|
|
||||||
|
data = response.json()
|
||||||
|
if refresh_token:
|
||||||
|
_refresh_token_cache.invalidate(refresh_token)
|
||||||
|
_refresh_token_cache(refresh_token, data["refresh_token"])
|
||||||
|
return "Bearer " + data["access_token"]
|
||||||
|
|
||||||
def _call(self, endpoint, params=None, expect_error=False):
|
def _call(self, endpoint, params=None, expect_error=False):
|
||||||
"""Call an API endpoint"""
|
"""Call an API endpoint"""
|
||||||
@@ -498,6 +504,11 @@ class DeviantartAPI():
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@cache(maxage=365*24*60*60, keyarg=0)
|
||||||
|
def _refresh_token_cache(original_token, new_token=None):
|
||||||
|
return new_token or original_token
|
||||||
|
|
||||||
|
|
||||||
SHADOW_TEMPLATE = """
|
SHADOW_TEMPLATE = """
|
||||||
<span class="shadow">
|
<span class="shadow">
|
||||||
<img src="{src}" class="smshadow" width="{width}" height="{height}">
|
<img src="{src}" class="smshadow" width="{width}" height="{height}">
|
||||||
|
|||||||
Reference in New Issue
Block a user