From 65773263fc9326531d71f0648b8fb0d80bcbb157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 10 Feb 2018 21:56:13 +0100 Subject: [PATCH] [util] implement OAuthSession.urlencode() (closes #75) - Python's own urllib.parse.urlencode() has no quote_via argument in Python 3.3 and 3.4, which is necessary to follow OAuth 1.0 quoting rules. --- gallery_dl/util.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 182a3df5..7fcb6dac 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -510,8 +510,7 @@ class OAuthSession(): def sign(self, url, params): """Generate 'oauth_signature' value and return query string""" - query = urllib.parse.urlencode( - sorted(params.items()), quote_via=self.quote) + query = self.urlencode(params) message = self.concat("GET", url, query).encode() key = self.concat(self.consumer_secret, self.token_secret).encode() signature = hmac.new(key, message, hashlib.sha1).digest() @@ -529,3 +528,11 @@ class OAuthSession(): @staticmethod def quote(value, _=None, encoding=None, errors=None): return urllib.parse.quote(value, "~", encoding, errors) + + @staticmethod + def urlencode(params): + quote = OAuthSession.quote + return "&".join([ + quote(str(key)) + "=" + quote(str(value)) + for key, value in sorted(params.items()) + ])