Skip to content

Commit

Permalink
write DeviantArt refresh-tokens to cache (#616)
Browse files Browse the repository at this point in the history
Writing the token is currently disabled by default and must be
enabled with 'extractor.oauth.cache'.

'extractor.deviantart.refresh-token' must be set to '"cache"'
to use the cached token.
  • Loading branch information
mikf committed Feb 25, 2020
1 parent 2a4f227 commit 913b833
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions gallery_dl/extractor/deviantart.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,15 @@ def __init__(self, extractor):
self.folders = extractor.config("folders", False)
self.metadata = extractor.extra or extractor.config("metadata", False)

self.refresh_token = extractor.config("refresh-token")
self.client_id = extractor.config("client-id", self.CLIENT_ID)
self.client_id = extractor.config(
"client-id", self.CLIENT_ID)
self.client_secret = extractor.config(
"client-secret", self.CLIENT_SECRET)

self.refresh_token = extractor.config("refresh-token")
if self.refresh_token == "cache":
self.refresh_token = "#" + str(self.client_id)

self.log.debug(
"Using %s API credentials (client-id %s)",
"default" if self.client_id == self.CLIENT_ID else "custom",
Expand Down
8 changes: 7 additions & 1 deletion gallery_dl/extractor/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _oauth1_authorization_flow(
def _oauth2_authorization_code_grant(
self, client_id, client_secret, auth_url, token_url,
scope="read", key="refresh_token", auth=True,
message_template=None):
message_template=None, cache=None):
"""Perform an OAuth2 authorization code grant"""

state = "gallery-dl_{}_{}".format(
Expand Down Expand Up @@ -162,6 +162,11 @@ def _oauth2_authorization_code_grant(
client_secret=client_secret,
))

# write to cache
if cache and config.get(("extractor", self.category), "cache"):
cache.update("#" + str(client_id), data[key])
self.log.info("Writing 'refresh-token' to cache")


class OAuthDeviantart(OAuthBase):
subcategory = "deviantart"
Expand All @@ -179,6 +184,7 @@ def items(self):
"https://www.deviantart.com/oauth2/authorize",
"https://www.deviantart.com/oauth2/token",
scope="browse",
cache=deviantart._refresh_token_cache,
)


Expand Down

0 comments on commit 913b833

Please sign in to comment.