X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/d20da7f3e83b0c68402d46ef96c463c307cd9e2f..098660ce6ca34597e97f074b3af48f64dcf3ea03:/twitter/oauth.py diff --git a/twitter/oauth.py b/twitter/oauth.py index bf81f42..be47e96 100644 --- a/twitter/oauth.py +++ b/twitter/oauth.py @@ -1,16 +1,48 @@ +from __future__ import print_function + from twitter.auth import Auth from time import time from random import getrandbits -from time import time -import urllib + +try: + import urllib.parse as urllib_parse +except ImportError: + import urllib2 as urllib_parse + import hashlib import hmac +import base64 + + + +def write_token_file(filename, oauth_token, oauth_token_secret): + """ + Write a token file to hold the oauth token and oauth token secret. + """ + oauth_file = open(filename, 'w') + print(oauth_token, file=oauth_file) + print(oauth_token_secret, file=oauth_file) + oauth_file.close() + +def read_token_file(filename): + """ + Read a token file and return the oauth token and oauth token secret. + """ + f = open(filename) + return f.readline().strip(), f.readline().strip() -# An OAuth implementation with various helpers... class OAuth(Auth): + """ + An OAuth authenticator. + """ def __init__(self, token, token_secret, consumer_key, consumer_secret): + """ + Create the authenticator. If you are in the initial stages of + the OAuth dance and don't yet have a token or token_secret, + pass empty strings for these params. + """ self.token = token self.token_secret = token_secret self.consumer_key = consumer_key @@ -19,23 +51,26 @@ class OAuth(Auth): def encode_params(self, base_url, method, params): params = params.copy() - params['oauth_token'] = self.token + if self.token: + params['oauth_token'] = self.token + params['oauth_consumer_key'] = self.consumer_key params['oauth_signature_method'] = 'HMAC-SHA1' params['oauth_version'] = '1.0' params['oauth_timestamp'] = str(int(time())) params['oauth_nonce'] = str(getrandbits(64)) - enc_params = urlencode_noplus(sorted(params.iteritems())) + enc_params = urlencode_noplus(sorted(params.items())) - key = self.consumer_secret + "&" + urllib.quote(self.token_secret, '') + key = self.consumer_secret + "&" + urllib_parse.quote(self.token_secret, '') message = '&'.join( - urllib.quote(i, '') for i in [method.upper(), base_url, enc_params]) - - signature = hmac.new( - key, message, hashlib.sha1).digest().encode('base64')[:-1] - return enc_params + "&" + "oauth_signature=" + urllib.quote(signature, '') + urllib_parse.quote(i, '') for i in [method.upper(), base_url, enc_params]) + + signature = (base64.b64encode(hmac.new( + key.encode('ascii'), message.encode('ascii'), hashlib.sha1) + .digest())) + return enc_params + "&" + "oauth_signature=" + urllib_parse.quote(signature, '') def generate_headers(self): return {} @@ -47,18 +82,18 @@ class OAuth(Auth): def urlencode_noplus(query): if hasattr(query,"items"): # mapping objects - query = query.items() - + query = list(query.items()) + encoded_bits = [] for n, v in query: # and do unicode here while we are at it... - if isinstance(n, unicode): + if isinstance(n, str): n = n.encode('utf-8') else: n = str(n) - if isinstance(v, unicode): + if isinstance(v, str): v = v.encode('utf-8') else: v = str(v) - encoded_bits.append("%s=%s" % (urllib.quote(n, ""), urllib.quote(v, ""))) + encoded_bits.append("%s=%s" % (urllib_parse.quote(n, ""), urllib_parse.quote(v, ""))) return "&".join(encoded_bits)