无法通过Twitter进行身份验证(OAuth问题)

2024-04-19 23:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为Twitter OAuth身份验证编写Python代码。当我试图请求令牌时,得到一个“401未授权”错误代码。你知道吗

在尝试诊断我的问题的过程中,我将经历身份验证过程的每一步,并试图掩盖我所犯的任何错误。关于生成“签名基字符串”,我发现了一个在线工具,它试图帮助验证签名基字符串:http://quonos.nl/oauthTester/

当我使用这个工具时,它会抱怨:

Bad URL encoding!

Both key and value in the POST body need to be URL encoded.

下面是我的Python代码生成的签名基字符串示例:

POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Foauth%2Frequest_token&oauth_callback%3Doob%26oauth_consumer_key%3DeXL46FKblmfiXHvmC3wcew%26oauth_nonce%3DTAHTO%2FmlyeJ1x9FrgFixosZPYVhvWLXmq%2BdKKTL1rTY%3D%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1391813822%26oauth_version%3D1.0

当我把这个字符串粘贴到验证器中时,它说:

    Bad URL encoding!

    Both key and value in the POST body need to be URL encoded.

    In this case: "TAHTO/mlyeJ1x9FrgFixosZPYVhvWLXmq+dKKTL1rTY" is bad

我非常困惑,因为URL中的所有键/值对实际上都是URL编码的(我假设“URL encoded”在这里是“percent encoded”的意思)

我的底弦有什么问题吗?你知道吗

编辑:

我发送到Twitter请求令牌的实际HTTP请求头是:

POST /1.1/oauth/request_token HTTP/1.1
Accept-Encoding: identity
Content-Length: 0
Connection: close
Accept: */*
User-Agent: Python-urllib/3.2
Host: api.twitter.com
Content-Type: application/x-www-form-urlencoded format
Authorization: OAuth oauth_callback="oob", oauth_consumer_key="eXL46FKblmfiXHvmC3wcew", oauth_nonce="nBcVYSqv8FEi0d7MEs8%2BqtqvdYA9JcbnW%2BVqoP%2FGlrI%3D", oauth_signature="WT9c3U5Puam7dEnMt3DWDsyVAHw%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1391815422", oauth_version="1.0"

Tags: 工具key字符串身份验证url过程twitterpost