如何调用 twitter API 使用 Javascript

How call twitter API Using Javascript

我已经在 Twitter 帐户中创建了一个应用程序。我试图获取特定主题标签的推文,但我不知道如何 运行 URL 并在此使用访问令牌。而且我找不到 JS SDK。我是 Twitter 的新手,对此感到困惑。

我找到了这段代码,但出现了 400 错误请求错误。请帮助我

 $.ajax({
        url: "https://api.twitter.com/1.1/statuses/user_timeline.json?",
        type: "GET",
        data: {count:10 },//cursor: "-1", 
               // screen_name: "sitestreams" },
        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");
        }
        xhr.setRequestHeader('Authorization: OAuth oauth_consumer_key="qqqq7a3ansbRqqqTfOBdX4hn2sQJp72a", oauth_nonce="a3df1878qqqqe0b6d644f6ac0fd3a354725a", oauth_signature="%qqqqq2BhZ4eYIjnJc0fcHU2VHhrjvR0Ns%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1435qqqqq579396", oauth_token="468141855-Id6phQoOE66n9CBzAdqqqqwEUAuQI2qqqqTBmxGyUHL9xarx", oauth_version="1.0"');
    }

如何使用这些签名

OAuth 签名结果

Important: This will only be valid for a few minutes. Also remember the cURL command will actually execute the request. null nullSignature base string null nullGET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Ffriends%2Flist.json&amp%253Binclude_user_entities%3Dfalse%26amp%253Bscreen_name%3Dtwitterapi%26amp%253Bskip_status%3Dtrue%26cursor%3D-1%26oauth_consumer_key%3D7a3addsssdnsbRTfOBdX4hn2sQJp72a%26oauth_nonce%3D84d92acbc6ssssddd8ff3ecd2fa72e8dc64c4fc%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1445456635455642473%26oauth_token%3D468141855-Id6phQoOE66n9CBzAdwsddEUAuQI2TBmxGyUHL9xarx%26oauth_version%3D1.0 null nullAuthorization header null nullAuthorization: OAuth oauth_consumer_key="7a3ansbRsssssTfOBdX4hn2sQJp72a", oauth_nonce="84d92acbsssc68ff3ecd2fa72e8dc64c4fc", oauth_signature="DThJZIUNssDNA8rtHt7R6x04aAGVM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1435642473", oauth_token="468145641855-Id6phQoOE66ssn9CBzAdwEUAssuQI2TBmxGyUHL9xarx", oauth_version="1.0" null nullcURL command null nullcurl --get 'https://api.twitter.com/1.1/friends/list.json' --data 'amp%3Binclude_user_entities=false&amp%3Bscreen_name=twitterapi&amp%3Bskip_status=true&cursor=-1' --header 'Authorization: OAuth oauth_consumer_key="7a3ansbRTsssfOBdX4hn2sQJp72a", oauth_nonce="84d9ss2acbc68ff3ecd2fa72e8dc64c4fc", oauth_signature="DThJZIUNDssssNA8rtHt7R6x04aAGVM%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="143564542473", oauth_token="468141851235-Id6phQoOE66n9CBsssszAdwEUAuQI2TBmxGyUHL9xarx", oauth_version="1.0"' --verbose null null

您没有正确处理此问题,我对您收到 400 错误并不感到惊讶。您正在做的是请求一个 OAuth 标志,该标志仅在手动执行后有效(通过 Twitter 开发者应用程序)。

我建议您对 JS 使用 twit,并使用 T.get 查找主题标签。您的结果仍会显示在 json 中,您可以从那里提取它们。