Note that there are some explanatory texts on larger screens.

plurals
  1. POTwitter OAuth authentication in javascript
    primarykey
    data
    text
    <p>I have looked everywhere for a good example of a JQuery ajax call to authenticate a use in Twitter using their OAuth method. I read the instructions several times and this is what I have so far, at the moment I am only trying to get the Oauth_Token to send the user to validate. I keep getting the error 401 unauthorized error. I have tried a few combinations changing the dataType and couple other things in the ajax call but no success. I believe that the "Create base signature and the Create signature from SHA1 encryption" is correct, but I could be wrong. If anyone has implement twitter OAuth in javascript successfully please let me know how. I am also not interested in using a third party API, I actually want to learn this. Thanks in advance for any help.</p> <p>Twitter instructions <a href="http://dev.twitter.com/pages/auth" rel="noreferrer">http://dev.twitter.com/pages/auth</a></p> <p>My code (Javascript)</p> <pre><code> //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" + "&amp;" + encodeURIComponent("https://api.twitter.com/oauth/request_token").toString() + "&amp;" + 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"); } </code></pre> <p>Here is the HTTP response</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload