0

我厌倦了将request-oauth库(基于python-request)移植到 Python 3(在 2to3 的帮助下),但是我在使用 StatusNet 验证签名时遇到了问题(与 Twitter 相同的 API)。

当我向 发出请求时oauth/request_token,我没有问题,但oauth/access_token我有一个错误401 Invalid signature。我不明白为什么,因为在我看来,我签署的内容是正确的。

例如,使用 python 2 代码,cf hook.pyauth.py(原始来自 git repo),我得到:

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.keyandtoken.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获得应用程序的授权并获得一个密码。之后我可以做有问题的请求

...
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})

auth.py 文件中的签名代码是

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 代码吗?

谢谢

4

2 回答 2

1

您可能需要验证请求中的 Authorization 标头字符串。通常它将是以下格式:

'授权' => '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。您可以使用此工具对字符串进行解码。

这对我有用。希望它可以帮助某人。

于 2015-10-09T12:02:16.340 回答
0

找到了答案!POST 请求中有两个oauth_verifier,导致签名错误...

于 2012-03-21T09:59:51.303 回答