OAuth身份验证,签名无效

OAuth身份验证,签名无效,oauth,python-3.x,twitter-oauth,python-2to3,Oauth,Python 3.x,Twitter Oauth,Python 2to3,我尝试将库(基于)移植到Python3(借助2to3),但在使用StatusNet(与Twitter相同的API)验证签名时遇到了问题 当我向oauth/request\u-token发出请求时,除了向oauth/access\u-token发出请求外,我没有问题。我遇到了一个错误401无效签名。我不明白为什么,因为在我看来,我的签名是正确的 例如,使用python 2代码cf和(源于git repo),我得到: 通过我的python 3端口,cf和,我得到: 在我看来两者都不错,但第一个成功,

我尝试将库(基于)移植到Python3(借助2to3),但在使用StatusNet(与Twitter相同的API)验证签名时遇到了问题

当我向
oauth/request\u-token
发出请求时,除了向
oauth/access\u-token
发出请求外,我没有问题。我遇到了一个错误
401无效签名
。我不明白为什么,因为在我看来,我的签名是正确的

例如,使用python 2代码cf和(源于git repo),我得到:

通过我的python 3端口,cf和,我得到:

在我看来两者都不错,但第一个成功,第二个返回401错误,无效签名

在这两种情况下,我都会得到
token.key
token.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
以获得对应用程序的授权并获得pincode。然后我可以执行有问题的请求

...
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代码一起工作吗


谢谢

找到答案!POST请求中有两个
oauth\u验证程序
,导致签名错误…

您可能需要验证请求中的授权标头字符串。通常格式为:

“授权”=>“OAuth realm=、oauth_timestamp=“1243392158”、oauth_nonce=“VsaPHb”、oauth_consumer_key=“XXXXXXXXXXXX”、oauth_token=“xxxxxx-XXXXXXXXXXXXXXXX”、oauth_version=“1.0”、oauth_签名方法=“HMAC-SHA1”、oauth_签名=“XXXXXXXXXXXXXXXXXXXXX”'

在上面的标题值中,检查“oauth_签名”是否已正确解码。也就是说,它不应包含以下值:%3D。您可以使用来解码字符串

这对我来说很有用。希望对别人有帮助

...
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]