[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.
This commit is contained in:
Mike Fährmann 2018-02-10 21:56:13 +01:00
parent 7e0207bcf4
commit 65773263fc
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -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())
])