2011-07-14 75 views
5

我已到處尋找JQuery ajax調用的一個很好的示例,以使用他們的OAuth方法在Twitter中對使用進行身份驗證。我多次閱讀說明,這是我迄今爲止,目前我只是試圖讓Oauth_Token發送用戶驗證。我不斷收到錯誤401未經授權的錯誤。我嘗試了一些組合,改變dataType並在ajax調用中耦合其他東西,但沒有成功。我相信「創建基本簽名和SHA1加密創建簽名」是正確的,但我可能是錯的。如果任何人已經成功實現了javascript OAuth請讓我知道如何。我對使用第三方API也不感興趣,我真的很想學習這一點。預先感謝您的幫助。Twitter中的OAuth身份驗證

Twitter的指令http://dev.twitter.com/pages/auth

我的代碼(JavaScript)的

//Unix time 
    var foo = new Date; // Generic JS date object 
    var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch 
    var unixtime = parseInt(unixtime_ms/1000); 

    var callBackURL = "oob"; //oob for now 
    var nonce = "12342897"; 

    //Create Signature Base String using formula 
    var baseSign = "POST" + "&" + encodeURIComponent("https://api.twitter.com/oauth/request_token").toString() + "&" 
    + encodeURIComponent("oauth_callback") + "%3D" + encodeURIComponent(callBackURL) 
    + "%26" 
    + encodeURIComponent("oauth_consumer_key") + "%3D" + encodeURIComponent("*****consumer key**********") 
    + "%26" 
    + encodeURIComponent("oauth_nonce") + "%3D" + encodeURIComponent(nonce) 
    + "%26" 
    + encodeURIComponent("oauth_signature_method") + "%3D" + encodeURIComponent("HMAC-SHA1") 
    + "%26" 
    + encodeURIComponent("oauth_timestamp") + "%3D" + encodeURIComponent(unixtime) 
    + "%26" 
    + encodeURIComponent("oauth_version") + "%3D" + encodeURIComponent("1.0"); 

    //Create Signature, With consumer Secret key we sign the signature base string 
    var signature = b64_hmac_sha1("********secrete key****************", baseSign); 

    //Build headers from signature 
    var jsonData = JSON.stringify({ 
     Authorization: { 
      oauth_nonce: nonce, 
      oauth_callback: encodeURIComponent(callBackURL), 
      oauth_signature_method: "HMAC-SHA1", 
      oauth_timestamp: unixtime, 
      oauth_consumer_key: "**********consumer key*********", 
      oauth_signature: signature, 
      oauth_version: "1.0" 
     } 
    }); 

    //Request Access Token 
    $.ajax({ 
     url: "http://api.twitter.com/oauth/request_token", 
     type: "post", 
     headers: jsonData, 
     dataType: "jsonp", 
     success: function (data) { 
      alert(data); 
     }, 
     error: function (data) { 
      alert("Error"); 
     } 

這裏是HTTP響應

HTTP/1.1 401 Unauthorized 
Date: Thu, 14 Jul 2011 21:05:55 GMT 
Server: hi 
Status: 401 Unauthorized 
X-Transaction: 1310677555-60750-49846 
X-Frame-Options: SAMEORIGIN 
Last-Modified: Thu, 14 Jul 2011 21:05:55 GMT 
X-Runtime: 0.01152 
Content-Type: text/html; charset=utf-8 
Pragma: no-cache 
X-Revision: DEV 
Expires: Tue, 31 Mar 1981 05:00:00 GMT 
X-MID: ffc990286a86b688f1e72ef733365926ea30ca62 
Vary: Accept-Encoding 
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0, proxy-revalidate 
Content-Length: 44 
Proxy-Connection: Keep-Alive 
Connection: Keep-Alive 
Set-Cookie: lang=en; path=/ 
Set-Cookie: lang=en; path=/ 
Set-Cookie: _twitter_sess=BAh7DjoOcmV0dXJuX3RvIiJodHRwOi8vdHdpdHRlci5jb20vcGF1bF9pcmlz%250AaDoJdXNlcmkEl6phBDoVaW5fbmV3X3VzZXJfZmxvdzA6D2NyZWF0ZWRfYXRs%250AKwgE9BTnMAE6B2lkIiU2MzBkYmJmNmY4YmRmY2E3NGI2NDRmZTFhNjZiNmU2%250ANToTc2hvd19oZWxwX2xpbmswOgxjc3JmX2lkIiUyODIzYWNlNmFjMzQ3OTk5%250AZDE5YmVlYmJlYzM4MTg0ZSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7ADoTcGFzc3dvcmRfdG9rZW4i%250ALThkY2NlMmE1OTI5OWJmM2VjNDI4YWRiOTE0YTRmYzVjYzQwN2FkMmM%253D--22a361f562df3aa6991b81fff6f486086ab71f0e; 
domain=.twitter.com; 
path=/; HttpOnly 
Proxy-support: Session-based-authentication 
Age: 0 

Failed to validate oauth signature and token 
+0

你有沒有找到解決方案?我現在有同樣的問題,如果有可能,這將是很好的。 – Gus

回答

3

我有完全一樣的問題。輸在Twitter的開發者頁面的幾個小時後,我發現這一點:

https://dev.twitter.com/discussions/3454

Basicly你不能得到的oauth_token客戶端上的這種方式。這就是爲什麼你總是得到錯誤401.

有一個解決方案,以避免服務器端使用'xauth'(https://dev.twitter.com/docs/oauth/xauth),但這樣做,你需要向[email protected]發送詳細消息以請求xAuth權限。

0

3-leggedo OAuth的目的是讓用戶授權您的應用程序訪問他的數據。您不能以編程方式發出請求令牌和訪問令牌。請求令牌必須由用戶通過他的瀏覽器授權,然後才能交換並獲得訪問令牌。