Skip to content

Commit

Permalink
Modified OAuth params initialize method (support multi thread
Browse files Browse the repository at this point in the history
  • Loading branch information
techno committed Jul 4, 2010
1 parent 8c0d2b4 commit 6742817
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions twoauth/oauth.py
Expand Up @@ -7,7 +7,7 @@
# - http://www.techno-st.net/wiki/python-twoauth
#
#
# Copyright (c) 2009 Hirotaka Kawata
# Copyright (c) 2009-2010 Hirotaka Kawata
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -61,11 +61,15 @@ def __init__(self, ckey, csecret, atoken = "", asecret = "",

# Get Request Token
def request_token(self):
self._init_params()
del self.params["oauth_token"]
# initialize OAuth parameters
oauth_params = self._init_params()
del oauth_params["oauth_token"]

auth_header = self.oauth_header(self.reqt_url)

# get OAuth header
auth_header = self.oauth_header(self.reqt_url,
oauth_params = oauth_params)

# send request
req = urllib2.Request(self.reqt_url)
req.add_header("Authorization", auth_header)
resp = urllib2.urlopen(req)
Expand All @@ -84,14 +88,19 @@ def authorize_url(self, token_info):

# Get Access Token
def access_token(self, token_info, pin):
# set request token information
token = token_info["oauth_token"]
secret = token_info["oauth_token_secret"]

self._init_params(token)
self.params["oauth_verifier"] = pin

auth_header = self.oauth_header(self.acct_url, secret = secret)

# initialize OAuth parameters
oauth_params = self._init_params(token)
oauth_params["oauth_verifier"] = pin

# get OAuth header
auth_header = self.oauth_header(self.acct_url, secret = secret,
oauth_params = oauth_params)

# send request
req = urllib2.Request(self.acct_url)
req.add_header("Authorization", auth_header)
resp = urllib2.urlopen(req)
Expand All @@ -101,26 +110,34 @@ def access_token(self, token_info, pin):
for p in token_info:
token_info[p] = token_info[p][0]

# set token and secret to instance if not set
if not self.atoken and not self.asecret:
self.atoken = token_info["oauth_token"]
self.asecret = token_info["oauth_token_secret"]

return token_info

# Return Authorization Header String
def oauth_header(self, url, method = "GET", add_params = {}, secret = ""):
sig = self._make_signature(url, method, secret, add_params)
self.params["oauth_signature"] = sig
def oauth_header(self, url, method = "GET", add_params = {}, secret = "", oauth_params = None):
# initialize OAuth parameters if no given oauth_params
if oauth_params == None:
oauth_params = self._init_params()

# get oauth_signature
sig = self._make_signature(url, oauth_params, method, secret, add_params)
oauth_params["oauth_signature"] = sig

# quote OAuth format
plist = []
for p in self.params:
for p in oauth_params:
plist.append('%s="%s"' % (
self._oquote(p), self._oquote(self.params[p])))
self._oquote(p), self._oquote(oauth_params[p])))

return "OAuth %s" % (", ".join(plist))

# Return urllib2.Request Object for OAuth
def oauth_request(self, url, method = "GET", add_params = {}):
# quote parameters
enc_params = {}
if add_params:
api_params = urllib.urlencode(add_params)
Expand All @@ -129,6 +146,7 @@ def oauth_request(self, url, method = "GET", add_params = {}):
else:
api_params = ""

# create urllib2.Request
if method == "GET":
if add_params:
req = urllib2.Request("%s?%s" % (url, api_params))
Expand All @@ -139,12 +157,13 @@ def oauth_request(self, url, method = "GET", add_params = {}):
else:
raise

# set OAuth header
req.add_header("Authorization", self.oauth_header(
url, method, enc_params, secret = self.asecret))

return req

# Return httplib.HTTPResponse (for DELETE Method
# Return httplib.HTTPResponse (for DELETE Method and Streaming API
def oauth_http_request(self, url, method = "GET", add_params = {}):
enc_params = {}
if add_params:
Expand All @@ -163,30 +182,32 @@ def oauth_http_request(self, url, method = "GET", add_params = {}):

return con.getresponse()

# Get random string (for oauth_nonce)
def _rand_str(self, n):
seq = string.ascii_letters + string.digits
return ''.join(random.choice(seq) for i in xrange(n))

# Initialize OAuth parameters
def _init_params(self, token = None):
if token == None:
token = self.atoken

self.params = {
params = {
"oauth_consumer_key": self.ckey,
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": str(int(time.time())),
"oauth_nonce": self._rand_str(10),
"oauth_version": "1.0",
"oauth_token" : token
}

return params

def _make_signature(self, url, method = "GET",
# calculate oauth_signature
def _make_signature(self, url, oauth_params, method = "GET",
secret = "", add_params = {}):
# init OAuth params
self._init_params()

sigparams = {}
sigparams.update(self.params)
sigparams.update(oauth_params)
sigparams.update(add_params)

# Generate Signature Base String
Expand All @@ -205,6 +226,7 @@ def _make_signature(self, url, method = "GET",

return sig

# quote string for OAuth format
def _oquote(self, s):
return urllib.quote(str(s), "-._~")

Expand Down

0 comments on commit 6742817

Please sign in to comment.