from time import time
from random import getrandbits
from time import time
-import urllib
+import urllib.request, urllib.parse, urllib.error
import hashlib
import hmac
import base64
Write a token file to hold the oauth token and oauth token secret.
"""
oauth_file = open(filename, 'w')
- print >> oauth_file, oauth_token
- print >> oauth_file, oauth_token_secret
+ print(oauth_token, file=oauth_file)
+ print(oauth_token_secret, file=oauth_file)
oauth_file.close()
def read_token_file(filename):
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])
+ 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.quote(signature, '')
+ return enc_params + "&" + "oauth_signature=" + urllib.parse.quote(signature, '')
def generate_headers(self):
return {}
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)