php - 为什么 Twitter 返回 "Failed to validate oauth signature and token?"

标签 php twitter oauth

很抱歉给您带来另一个“无法验证 oauth 签名和 token ”错误,但我无法弄清楚我的请求有什么问题。

我正在从这个字符串构建我的签名:

POST&http%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_callback%3Dhttp%3A%2F%2Fcraiga.id.au%2Ftwitter%2Fconnected%26oauth_consumer_key%3Dtm5...DOg%26oauth_nonce%3D8...22b%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1275453048%26oauth_version%3D1.0

由此,我使用以下 PHP 代码生成了一个 28 个字符的签名:

base64_encode(hash_hmac('sha1', $raw, 'YUo...HIU' . '&', true));

使用此签名,我发送以下请求:

POST http://api.twitter.com/oauth/request_token HTTP/1.1
Host: api.twitter.com
Pragma: no-cache
Accept: */*
Proxy-Connection: Keep-Alive
Authorization: OAuth oauth_nonce="3D8...22b", oauth_callback="http%3A%2F%2Fcraiga.id.au%2Ftwitter%2Fconnected", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1275453048", oauth_consumer_key="tm5...DOg", oauth_signature="aYd...c6E%3D", oauth_version="1.0"
Content-Length: 266
Content-Type: application/x-www-form-urlencoded

oauth_callback=http%3A%2F%2Fcraiga.id.au%2Ftwitter%2Fconnected&oauth_consumer_key=tm5...DOg&oauth_nonce=3D8...22b&oauth_signature_method=HMAC-SHA1&oauth_timestamp= 1275453048&oauth_version=1.0

我从 Twitter 收到了对此请求的以下响应:

HTTP/1.1 401 Unauthorized
Date: Wed, 02 Jun 2010 04:40:14 GMT
Server: hi
Status: 401 Unauthorized
X-Transaction: 1275453614-48409-7443
Last-Modified: Wed, 02 Jun 2010 04:40:14 GMT
X-Runtime: 0.01083
Content-Type: text/html; charset=utf-8
Content-Length: 44
Pragma: no-cache
X-Revision: DEV
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Set-Cookie: k=58.161.42.101.1275453614748615; path=/; expires=Wed, 09-Jun-10 04:40:14 GMT; domain=.twitter.com
Set-Cookie: guest_id=12754536147577949; path=/; expires=Fri, 02 Jul 2010 04:40:14 GMT
Set-Cookie: _twitter_sess=BAh7CToPY3JlYXRlZF9hdGwrCKaq9fYoAToRdHJhbnNfcHJvbXB0MDoHaWQi%250AJWU0ZDFhMGQzMWU0NTZjMzJiZWFkNWUzMTA4ZDRjOTg3IgpmbGFzaElDOidB%250AY3Rpb25Db250cm9sbGVyOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--f1e5c7649858a1694f24307504354846bbc1d16b; domain=.twitter.com; path=/
Vary: Accept-Encoding
Connection: close

Failed to validate oauth signature and token

如果有人能解释为什么这可能会失败,我很乐意听到。

最佳答案

您使用了错误的信息来生成签名。 你应该使用 ...

oauth_callback=http%3A%2F%2Fcraiga.id.au%2Ftwitter%2Fconnected&oauth_consumer_key=tm5...DOg&oauth_nonce=3D8...22b&oauth_signature_method=HMAC-SHA1&oauth_timestamp= 1275453048&oauth_version=1.0

... 生成签名(阅读:不使用“POST”和请求 URI)

有关详细信息...请参阅 Twitter Developers: Creating a signature

关于php - 为什么 Twitter 返回 "Failed to validate oauth signature and token?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2955087/

相关文章:

python - api = twitter.Api() AttributeError : 'module' object has no attribute 'Api

javascript - Node.js, 'X' 未定义

java - Java 中的 Yahoo OAuth 库?

java - 如何使用 redis 使用 spring-security-oauth2 持久化 token

Cordova 插件 InAppBrowser 无法第二次打开

php - magento资源中的version和data_version有什么区别?

php - 教义迁移问题

php - HTML 表单未将值传递给 PHP (mysqli_real_escape_string)

PHP:用 iconv 处理特殊字符

html - 从 HTML href 链接到 native 应用程序