【问题标题】:400 bad request on twitter oauth request tokentwitter oauth 请求令牌上的 400 错误请求
【发布时间】:2016-02-25 10:50:54
【问题描述】:

我正在尝试向https://api.twitter.com/oauth/request_token 发送 POST 请求。响应是 400 Bad Request,没有任何信息。这是我的代码:

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);
        }
    })
}

我读了几遍官方指南。我做错了什么?

我只注意到变量“signature”的值类似于“YTg5ZmI2ZmEwMWU4MDkzMjlkZmEzMmVmMmVmYzgxMjlmZTJlNDdlZQ==”并且不像“tnnArxj06cWHq44gCs1OSKk/jLY=”(如官方指南中所示),因为函数 btoa() 与 String 一起使用而不是整数。

【问题讨论】:

    标签: javascript extjs twitter oauth


    【解决方案1】:

    尝试将 async: false 更改为 true

    【讨论】:

    • 400 bad request 表示您的请求不正确,请使用开发者工具或 Firebug 检查浏览器中实际发送的请求(通过 ajax 调用),从那里您可以获得线索。
    • 我在打开这个主题之前已经这样做了。我检查了请求,没有发现任何错误。但响应仍然是 400 Bad Request。问题可能出在我制作的签名中。但我没有看到问题。
    • 希望对你有帮助
    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 2012-06-22
    • 2011-05-04
    • 2011-09-05
    • 2017-01-10
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    相关资源
    最近更新 更多