2011-09-05 65 views
5

到目前爲止,我一直在使用庫來處理OAuth,但最近我一直在深入地嘗試瞭解底層OAuth進程。目前,我正在使用OAuth 1.0a的這個簡單的代碼來連接Tumblr API v2在使用Python處理Tumblr API時無法獲得OAuth「請求令牌」

import urllib, urllib2, time, random, hmac, base64, hashlib 

def makenonce(): 
    random_number = ''.join(str(random.randint(0, 9)) for _ in range(40)) 
    m = hashlib.md5(str(time.time()) + str(random_number)) 
    return m.hexdigest() 

def encodeparams(s): 
    return urllib.quote(str(s), safe='~') 

# Actual key and secret from a test app created using a dummy Tumblr account 
consumer_key = '97oAujQhSaQNv4XDXzCjdZlOxwNyhobmDwmueJBCHWsFFsW7Ly' 
consumer_secret = '5q1dpF659SOgSUb0Eo52aAyoud8N8QOuJu6enCG92aDR6WoMlf' 

#oauth URLs 
request_tokenURL = 'http://www.tumblr.com/oauth/request_token' 

#oauth params 
oauth_parameters = { 
      'oauth_consumer_key'  : consumer_key, 
      'oauth_nonce'   : makenonce(), 
      'oauth_timestamp'  : str(int(time.time())), 
      'oauth_signature_method' : "HMAC-SHA1", 
      'oauth_version'   : "1.0" 
      } 

normalized_parameters = encodeparams('&'.join(['%s=%s' % (encodeparams(str(k)), encodeparams(str(oauth_parameters[k]))) for k in sorted(oauth_parameters)])) 
# Since I'm focusing only on getting the request token for now, I set this to POST. 
normalized_http_method = 'POST' 
normalized_http_url = encodeparams(request_tokenURL) 
signature_base_string = '&'.join([normalized_http_method, normalized_http_url, normalized_parameters]) 
oauth_key = consumer_secret + '&' 
hashed = hmac.new(oauth_key, signature_base_string, hashlib.sha1) 
oauth_parameters['oauth_signature'] = base64.b64encode(hashed.digest()) 
oauth_header = 'Authorization: OAuth realm="http://www.tumblr.com",' + 'oauth_nonce="' + oauth_parameters['oauth_nonce'] + '",' + 'oauth_timestamp="' + oauth_parameters['oauth_timestamp'] + '",' + 'oauth_consumer_key="' + oauth_parameters['oauth_consumer_key'] + '",' + 'oauth_signature_method="HMAC-SHA1",oauth_version="1.0",oauth_signature="' + oauth_parameters['oauth_signature'] +'"' 

# sample oauth_header generated by the code above: 
# Authorization: OAuth realm="http://www.tumblr.com",oauth_nonce="c200a0e06f30b84b851ac3e99a71054b",oauth_timestamp="1315231855",oauth_consumer_key="97oAujQhSaQNv4XDXzCjdZlOxwNyhobmDwmueJBCHWsFFsW7Ly",oauth_signature_method="HMAC-SHA1",oauth_version="1.0",oauth_signature="kVAlmwolCX0WJIvTF9MB2UV5rnU=" 


req = urllib2.Request(request_tokenURL) 
req.add_header('Authorization', oauth_header) 
# If all goes well, Tumblr should send me the oauth request token. 
print urllib2.urlopen(req).read() 

取而代之的是OAuth的請求記號,返回的tumblr HTTP 錯誤401:未經授權

事情我已經嘗試沒有成功:

  1. 從 「1.0」 到 「1.0A」 改變oauth_version,並再次改變了它。
  2. OAuth指南要求在consumer_secret的末尾添加'&'以獲得oauth_key。我後來嘗試刪除'&',看看是否有任何區別。
  3. 檢查OAuth參數是否已排序,並且它們是。
  4. 未將字符串「Authorization:」添加到oauth_header,然後稍後將其添加回來。沒有任何區別。

我哪裏出錯了?

+0

你可能不應該在這裏有你的密鑰 –

+0

@Cal這是一個專門爲這個問題製作的虛擬應用程序。它就在評論中:'//使用** dummy ** Tumblr帳戶創建的測試應用程序的實際密鑰和祕密。 – vjk2005

回答

4

使得在上面的代碼中僅2簡單的修改後解決了它:

  1. normalized_http_method = 'GET' #not POST
  2. oauth_header = 'OAuth realm="http://www...'#單詞 「授權」 是不必要的。我早先列出了「我嘗試過但沒有成功的事情」中列出的內容,但是(1)中列出的錯誤使我不了瞭解。隨着(1)的解決,我可以看到「授權」確實是不必要的。

基於OAuth的認證請求令牌的tumblr送我時,我終於得到了它的權利: oauth_token=mbRUgyDkPePfkEztiLELMqUl1kyNXEcaTCCwpb7SoXDF9mhiTF&oauth_token_secret=5pXllXGKA8orAaUat1G7ckIfMfYup8juMBAgEELUkeMZoC3pv6&oauth_callback_confirmed=true

這是一個一次性只是象徵性地和我在這裏列出它只是爲了爲了完整性。

+0

也許你應該看看這個:https://github.com/simplegeo/python-oauth2 – DocWiki