很抱歉讓您煩惱另一個「無法驗證oauth簽名和令牌」錯誤,但我無法弄清楚我的請求有什麼問題。爲什麼Twitter返回「無法驗證oauth簽名和令牌?」
我是從這個字符串構建我的簽名:
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
如果任何人都能說出爲什麼這可能會失敗,我很樂意聽到。
一個Twitter的開發人員倡導者已經[指出](http://groups.google.com/group/twitter-development-talk/browse_thread/thread/92f6fbc209b3463d/fe5226c33ecfdc74?#fe5226c33ecfdc74)我不應該'''''''oauth_ *'領域'以及包括他們在'Authorize'頭,但仍然沒有運氣與'GET'或空'POST'。 – 2010-06-03 01:07:51
請參閱:http://stackoverflow.com/questions/3295466/ – 2010-07-24 23:58:18
我承認這不是一個真正的答案,但我使用PECL OAuth軟件包解決了這個問題。 [Rasmus Lerdorf寫了一篇關於如何使用它的教程。](http://toys.lerdorf.com/archives/50-Using-pecloauth-to-post-to-Twitter.html) – 2010-07-22 02:40:09