I am trying to send a POST request to https://api.twitter.com/oauth/request_token. The response is 400 Bad Request with no info. Here is my code:
onTwitterRegisterClick: function() {
var callbackURL = 'http://test.loc:1841/',
consumerKey = 'm59nSEhyF3Zp4gVdpDzq6CIPp',
consumerSecret = 'Wou436sq4LUXwO1ajE2egdMdAfV9LtPLkG4JlCF4Yi5YpcrnTF',
requestTokenURL = 'https://api.twitter.com/oauth/request_token';
var time = new Date().valueOf().toString(),
oauth_nonce = makeRandomString(32);
var paramsForSignature = [
encodeURIComponent('oauth_callback') + '=' + encodeURIComponent(callbackURL),
encodeURIComponent('oauth_consumer_key') + '=' + encodeURIComponent(consumerKey),
encodeURIComponent('oauth_nonce') + '=' + encodeURIComponent(oauth_nonce),
encodeURIComponent('oauth_signature_method') + '=' + encodeURIComponent('HMAC-SHA1'),
encodeURIComponent('oauth_timestamp') + '=' + encodeURIComponent(time),
encodeURIComponent('oauth_version') + '=' + encodeURIComponent('1.0')
];
var paramsForSignatureStr = paramsForSignature.join('&');
var signatureBaseString = 'POST&' + encodeURIComponent(requestTokenURL) + '&' + encodeURIComponent(paramsForSignatureStr);
//alert(signatureBaseString);
var signature = btoa(CryptoJS.HmacSHA1(signatureBaseString, consumerSecret + '&'));
//var signature = prompt('hmac-sha1 of signatureBaseString=' + CryptoJS.HmacSHA1(signatureBaseString, consumerSecret + '&'));
//alert(signature);
Ext.Ajax.request({
method: 'POST',
url: requestTokenURL,
async: false,
headers:{
Authorization: 'OAuth oauth_callback="' + encodeURIComponent(callbackURL) + '", ' +
'oauth_consumer_key="' + consumerKey + '", ' +
'oauth_nonce="' + oauth_nonce + '", ' +
'oauth_signature="' + encodeURIComponent(signature) + '", ' +
'oauth_signature_method="HMAC-SHA1", ' +
'oauth_timestamp="' + time + '", ' +
'oauth_version="1.0"'
},
success: function(response, opts) {
alert(response.responseText);
},
failure: function (response, opts) {
alert(response.responseText);
}
})
}
I read the official guide a few times. What am I doing wrong?
I only noticed the value of the variable "signature" is like "YTg5ZmI2ZmEwMWU4MDkzMjlkZmEzMmVmMmVmYzgxMjlmZTJlNDdlZQ==" and is not like "tnnArxj06cWHq44gCs1OSKk/jLY=" (as in the official guide) because the function btoa() work with String and with not Integer.