1

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.

knoxx
  • 11
  • 2

1 Answers1

-2

try changing the async: false to true

Vinay Sinha
  • 193
  • 2
  • 13
  • 400 bad request means your request is not correct, please check what request(by ajax call) actually is being sent in browser using developer tools or Firebug, from there you can get clue. – Vinay Sinha Feb 25 '16 at 13:21
  • I had done this before I opened this topic. I'd checked the request and I'd not found any errors. But the response is still 400 Bad Request. The problem might be in the signature, which I make. But I am not seeing a problem. – knoxx Feb 25 '16 at 13:40
  • http://stackoverflow.com/questions/19546866/400-bad-request-response-for-ajax-request – Vinay Sinha Feb 25 '16 at 13:59
  • http://stackoverflow.com/questions/16017081/getting-400-bad-request-error-in-jquery-ajax-post – Vinay Sinha Feb 25 '16 at 13:59
  • hope it can help you – Vinay Sinha Feb 25 '16 at 14:00
  • http://stackoverflow.com/questions/20219994/400-bad-request-when-sending-http-post-request-to-get-token-from-auth-code – Vinay Sinha Feb 25 '16 at 14:02
  • Thanks a lot, Vinay. I'll see these links. – knoxx Feb 25 '16 at 14:54
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/11404082) – Alfred Huang Feb 26 '16 at 00:41
  • @fish_ball How does this not attempt to provide an answer? – Undo Feb 26 '16 at 01:05