admin管理员组文章数量:1315229
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 binations 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.
Twitter instructions
My code (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("").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: "",
type: "post",
headers: jsonData,
dataType: "jsonp",
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error");
}
Here is the HTTP response
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;
path=/; HttpOnly
Proxy-support: Session-based-authentication
Age: 0
Failed to validate oauth signature and token
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 binations 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.
Twitter instructions http://dev.twitter./pages/auth
My code (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./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./oauth/request_token",
type: "post",
headers: jsonData,
dataType: "jsonp",
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error");
}
Here is the HTTP response
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.;
path=/; HttpOnly
Proxy-support: Session-based-authentication
Age: 0
Failed to validate oauth signature and token
Share
Improve this question
asked Jul 14, 2011 at 21:23
Deylo WooDeylo Woo
611 gold badge1 silver badge6 bronze badges
1
- Did you ever find a solution? I'm having the same problem right now, it would be nice if it was possible. – Gus Commented Mar 8, 2013 at 5:08
2 Answers
Reset to default 3I had exactly the same issue. After lose a few hours at the twitter developers page i found this:
https://dev.twitter./discussions/3454
Basicly you can't get the oauth_token this way on the client side. That's why you are always getting error 401.
There's a solution to avoid server side using the 'xauth' (https://dev.twitter./docs/oauth/xauth), but to do so you need to send a detailed message to [email protected] to request xAuth privileges.
The purpose of the 3-leggedo OAuth is to get the user authorise your application's access to the his data. You cannot issue programmatically a Request Token and the an Access Token. The Request Token must be authorised by the user via his browser on a different flow before you can exchange it and get an Access Token.
本文标签: jqueryTwitter OAuth authentication in javascriptStack Overflow
版权声明:本文标题:jquery - Twitter OAuth authentication in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741982194a2408465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论