5

目标 - 使用 javascript 获取特定用户的 twitter 关注者

我已尝试将以下代码作为 POC-

$(document).ready(function() {
    // Handler for .ready() called.

    $.ajax({
        url: "https://api.twitter.com/1.1/followers/ids.json?callback=?",
        type: "GET",
        data: { cursor: "-1", 
                screen_name: "twitterapi" },
        cache: false,
        dataType: 'json',

        success: function(data) { alert('hello!'); console.log(data);},
        error: function(html) { alert(html); },
        beforeSend: setHeader
    });


    function setHeader(xhr) {
        if(xhr && xhr.overrideMimeType) {
            xhr.overrideMimeType("application/j-son;charset=UTF-8");
        }

        //var nonce = freshNonce();
        //var timestamp = freshTimestamp();
        //var signature = sign(nonce,timestamp);

        //alert(signature);
        //alert(accessToken+"-"+consumerKey);
        //alert(oauth_version+"-"+oauth_signature_method);
        xhr.setRequestHeader('Authorization','OAuth');
        xhr.setRequestHeader('oauth_consumer_key', 'HdFdA3C3pzTBzbHvPMPw');
        xhr.setRequestHeader('oauth_nonce', '4148fa6e3dca3c3d22a8315dfb4ea5bb');
        xhr.setRequestHeader('oauth_signature','uDZP2scUz6FUKwFie4FtCtJfdNE%3D');
        xhr.setRequestHeader('oauth_signature_method', 'HMAC-SHA1');
        xhr.setRequestHeader('oauth_timestamp', '1359955650');
        xhr.setRequestHeader('oauth_token', '1127121421-aPHZHQ5BCUoqfHER2UYhQYUEm0zPEMr9xJYizXl');
        xhr.setRequestHeader('oauth_version', '1.0');
    }

});

我从 Twitter OAuth 工具计算了签名值..这给了我 400 Bad Request 错误....

请让我知道问题是什么...

4

2 回答 2

4

问题是您的请求标头,应该是这样的:

xhr.setRequestHeader('Authorization','OAuth oauth_consumer_key="HdFdA3C3pzTBzbHvPMPw", oauth_nonce="4148fa6e3dca3c3d22a8315dfb4ea5bb", oauth_signature="uDZP2scUz6FUKwFie4FtCtJfdNE%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp= "1359955650", oauth_token, "1127121421-aPHZHQ5BCUoqfHER2UYhQYUEm0zPEMr9xJYizXl", oauth_version="1.0"');

顺便说一句,这个 javascript 库可能会帮助您了解 OAuth 的内容:oauth-1.0a

它支持客户端和node.js

干杯

于 2014-01-18T14:49:45.190 回答
1

oauth_* 字段都是 Authorization 标头字符串的一部分,因此需要将它们连接起来,如本页底部所示 - https://dev.twitter.com/docs/auth/authorizing-request

它们不应显示为单独的标题字段。

于 2013-10-02T16:21:27.613 回答