2012-03-20 126 views
0

我厭倦了將request-oauth庫(基於python-request)移植到Python 3(在2to3的幫助下),但我有問題需要使用StatusNet(與Twitter相同的API)驗證簽名。OAuth身份驗證,無效簽名

當我向oauth/request_token發出請求時,我沒有問題,但是要到oauth/access_token我有一個錯誤401 Invalid signature。我不明白爲什麼,因爲在我看來,我簽署的是正確的。

例如,與蟒蛇2碼,CF hook.pyauth.py(原從混帳回購協議),我得到:

signing_key = '0de1456373dfc9349dd38a48e61fc844&136d6b9a597ee57d4338254812681acd', 
signing_raw = 'POST&http%3A%2F%2Fstatus2.dotzero.me%2Fapi%2Foauth%2Faccess_token&oauth_consumer_key%3Dec3ad931b294b51a5ff595c732acb7a5%26oauth_nonce%3D33448267%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332279747%26oauth_token%3D2131043f3516bcb730d391ed2033a880%26oauth_verifier%3D8816492%26oauth_version%3D1.0' 
oauth_hook.token.key = '2131043f3516bcb730d391ed2033a880' 
oauth_hook.token.secret = '136d6b9a597ee57d4338254812681acd' 
request.data_and_params = {'oauth_version': '1.0', 'oauth_signature': 'xyjxH5QcfZXnG111L7qANZ+ahRI=', 
    'oauth_token': '2131043f3516bcb730d391ed2033a880', 'oauth_nonce': '33448267', 
    'oauth_timestamp': '1332279747', 'oauth_verifier': '8816492', 
    'oauth_consumer_key': 'ec3ad931b294b51a5ff595c732acb7a5', 
    'oauth_signature_method': 'HMAC-SHA1'} 

,並與我的Python 3端口,CF hook.pyauth.py,我得到:

signing_key = '0de1456373dfc9349dd38a48e61fc844&136d6b9a597ee57d4338254812681acd', 
signing_raw = 'POST&http%3A%2F%2Fstatus2.dotzero.me%2Fapi%2Foauth%2Faccess_token&oauth_consumer_key%3Dec3ad931b294b51a5ff595c732acb7a5%26oauth_nonce%3D52360702%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332278837%26oauth_token%3D2131043f3516bcb730d391ed2033a880%26oauth_verifier%3D8816492%26oauth_verifier%3D8816492%26oauth_version%3D1.0' 
oauth_hook.token.key = '2131043f3516bcb730d391ed2033a880' 
oauth_hook.token.secret = '136d6b9a597ee57d4338254812681acd' 
request.data_and_params = {'oauth_nonce': '52360702', 'oauth_timestamp': '1332278837', 
    'oauth_verifier': '8816492', 'oauth_consumer_key': 'ec3ad931b294b51a5ff595c732acb7a5', 
    'oauth_signature_method': 'HMAC-SHA1', 'oauth_version': '1.0', 
    'oauth_token': '2131043f3516bcb730d391ed2033a880', 
    'oauth_signature': 'BRsb11dk++405uaq5pRS+CMUzbo='} 

這兩個對我來說都不錯,但第一個成功,第二個返回401錯誤,簽名無效。

在這兩種情況下,我得到的token.keytoken.secret作爲的結果:

OAuthHook.consumer_key = self.ckey 
OAuthHook.consumer_secret = self.csecret 
oauth_hook = OAuthHook() 
client = requests.session(hooks={'pre_request': oauth_hook}) 
response = client.post('%soauth/request_token' % (self.url), {'oauth_callback': 'oob'}) 
# new oauth_hook with the request token 
oauth_hook = OAuthHook(response[b'oauth_token'][0],response[b'oauth_token_secret'][0]) 

他們,我去oauth/authorize?oauth_token=%s" % oauth_hook.token.key獲得授權的應用程序,並獲得pin碼。從那以後,我可以從auth.py文件做有問題的請求

... 
response = client.post('%soauth/request_token' % (self.url), {'oauth_callback': 'oob'}) 
oauth_hook = OAuthHook(response[b'oauth_token'][0],response[b'oauth_token_secret'][0]) 
# get the pincode from %soauth/authorize?oauth_token=%s" % (self.url, oauth_hook.token.key) 
oauth_hook.token.set_verifier(pincode) 
client = requests.session(hooks={'pre_request': oauth_hook}) 
response = client.post("%soauth/access_token" % (self.url), 
       {'oauth_verifier': pincode}) 

簽名代碼是

def sign(self, request, consumer, token): 
    """Builds the base signature string.""" 
    key, raw = self.signing_base(request, consumer, token) 
    hashed = hmac.new(key.encode(), raw.encode(), sha1) 
    # Calculate the digest base 64. 
    return binascii.b2a_base64(hashed.digest())[:-1] 

任何想法,爲什麼它不與py3k代碼工作?

謝謝

回答

0

找到了答案! POST請求中有兩個oauth_verifier,導致簽名錯誤...

1

您可能需要驗證請求中的授權標頭字符串。通常這將是以下格式:

'授權'=>「的OAuth 境界= 「」,oauth_timestamp = 「1243392158」,oauth_nonce = 「VsaPHb」,oauth_consumer_key = 「xxxxxxxxxxxxxxxxxx」,組oauth_token =「XXXXXX- XXXX-XXXXXXXXXXXXXX」,oauth_version = 「1.0」,oauth_signature_method = 「HMAC-SHA1」,oauth_signature = 「XXXXXXXXXXXXXXXXXXXX」」

在上述標頭值,檢查 「oauth_signature」 被適當地解碼。也就是說,它不應該包含以下值:%3D。您可以使用this tool來解碼字符串。

這對我有效。希望它能幫助別人。