[oauth] improve output

- show which api key / client id gets used (#3518)
- show in which browser authorization URLs gets opened in
This commit is contained in:
Mike Fährmann
2023-01-20 12:33:26 +01:00
parent df91ebb945
commit 568112dfbb

View File

@@ -68,11 +68,19 @@ class OAuthBase(Extractor):
def open(self, url, params, recv=None): def open(self, url, params, recv=None):
"""Open 'url' in browser amd return response parameters""" """Open 'url' in browser amd return response parameters"""
import webbrowser
url += "?" + urllib.parse.urlencode(params) url += "?" + urllib.parse.urlencode(params)
if not self.config("browser", True) or not webbrowser.open(url):
stdout_write( browser = self.config("browser", True)
"Please open this URL in your browser:\n\n" + url + "\n\n") if browser:
import webbrowser
browser = webbrowser.get()
if browser and browser.open(url):
self.log.info("Opening URL in %s:", browser.name.capitalize())
else:
self.log.info("Please open this URL in your browser:")
stdout_write("\n{}\n\n".format(url))
return (recv or self.recv)() return (recv or self.recv)()
def error(self, msg): def error(self, msg):
@@ -88,6 +96,10 @@ class OAuthBase(Extractor):
api_secret = self.oauth_config("api-secret") or default_secret api_secret = self.oauth_config("api-secret") or default_secret
self.session = oauth.OAuth1Session(api_key, api_secret) self.session = oauth.OAuth1Session(api_key, api_secret)
self.log.info("Using %s %s API key (%s)",
"default" if api_key == default_key else "custom",
self.subcategory, api_key)
# get a request token # get a request token
params = {"oauth_callback": self.redirect_uri} params = {"oauth_callback": self.redirect_uri}
data = self.session.get(request_token_url, params=params).text data = self.session.get(request_token_url, params=params).text
@@ -118,11 +130,18 @@ class OAuthBase(Extractor):
)) ))
def _oauth2_authorization_code_grant( def _oauth2_authorization_code_grant(
self, client_id, client_secret, auth_url, token_url, *, self, client_id, client_secret, default_id, default_secret,
scope="read", key="refresh_token", auth=True, auth_url, token_url, *, scope="read", duration="permanent",
cache=None, instance=None): key="refresh_token", auth=True, cache=None, instance=None):
"""Perform an OAuth2 authorization code grant""" """Perform an OAuth2 authorization code grant"""
client_id = str(client_id) if client_id else default_id
client_secret = client_secret or default_secret
self.log.info("Using %s %s client ID (%s)",
"default" if client_id == default_id else "custom",
instance or self.subcategory, client_id)
state = "gallery-dl_{}_{}".format( state = "gallery-dl_{}_{}".format(
self.subcategory, self.subcategory,
oauth.nonce(8), oauth.nonce(8),
@@ -133,7 +152,7 @@ class OAuthBase(Extractor):
"response_type": "code", "response_type": "code",
"state" : state, "state" : state,
"redirect_uri" : self.redirect_uri, "redirect_uri" : self.redirect_uri,
"duration" : "permanent", "duration" : duration,
"scope" : scope, "scope" : scope,
} }
@@ -143,13 +162,12 @@ class OAuthBase(Extractor):
# check authorization response # check authorization response
if state != params.get("state"): if state != params.get("state"):
self.send("'state' mismatch: expected {}, got {}.\n".format( self.send("'state' mismatch: expected {}, got {}.\n".format(
state, params.get("state") state, params.get("state")))
))
return return
if "error" in params: if "error" in params:
return self.error(params) return self.error(params)
# exchange the authorization code for a token # exchange authorization code for a token
data = { data = {
"grant_type" : "authorization_code", "grant_type" : "authorization_code",
"code" : params["code"], "code" : params["code"],
@@ -278,10 +296,10 @@ class OAuthDeviantart(OAuthBase):
yield Message.Version, 1 yield Message.Version, 1
self._oauth2_authorization_code_grant( self._oauth2_authorization_code_grant(
self.oauth_config( self.oauth_config("client-id"),
"client-id", deviantart.DeviantartOAuthAPI.CLIENT_ID), self.oauth_config("client-secret"),
self.oauth_config( deviantart.DeviantartOAuthAPI.CLIENT_ID,
"client-secret", deviantart.DeviantartOAuthAPI.CLIENT_SECRET), deviantart.DeviantartOAuthAPI.CLIENT_SECRET,
"https://www.deviantart.com/oauth2/authorize", "https://www.deviantart.com/oauth2/authorize",
"https://www.deviantart.com/oauth2/token", "https://www.deviantart.com/oauth2/token",
scope="browse user.manage", scope="browse user.manage",
@@ -298,7 +316,9 @@ class OAuthReddit(OAuthBase):
self.session.headers["User-Agent"] = reddit.RedditAPI.USER_AGENT self.session.headers["User-Agent"] = reddit.RedditAPI.USER_AGENT
self._oauth2_authorization_code_grant( self._oauth2_authorization_code_grant(
self.oauth_config("client-id", reddit.RedditAPI.CLIENT_ID), self.oauth_config("client-id"),
"",
reddit.RedditAPI.CLIENT_ID,
"", "",
"https://www.reddit.com/api/v1/authorize", "https://www.reddit.com/api/v1/authorize",
"https://www.reddit.com/api/v1/access_token", "https://www.reddit.com/api/v1/access_token",
@@ -325,6 +345,8 @@ class OAuthMastodon(OAuthBase):
application = self._register(self.instance) application = self._register(self.instance)
self._oauth2_authorization_code_grant( self._oauth2_authorization_code_grant(
application["client-id"],
application["client-secret"],
application["client-id"], application["client-id"],
application["client-secret"], application["client-secret"],
"https://{}/oauth/authorize".format(self.instance), "https://{}/oauth/authorize".format(self.instance),