0
我想學習和理解nodejs。當試圖連接到Twitter流和跟蹤鳴叫的API,我得到以下錯誤:Twitter流api
undefined:1
<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=ut
^
SyntaxError: Unexpected token <
at Object.parse (native)
at IncomingMessage.<anonymous> (/home/ytsejam/public_html/nodejs/11/twitter.js:15:20)
at IncomingMessage.emit (events.js:95:17)
at IncomingMessage.<anonymous> (_stream_readable.js:764:14)
at IncomingMessage.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:426:10)
at emitReadable (_stream_readable.js:422:5)
at readableAddChunk (_stream_readable.js:165:9)
at IncomingMessage.Readable.push (_stream_readable.js:127:10)
at HTTPParser.parserOnBody [as onBody] (http.js:142:22)
這裏是我的代碼,我嘗試連接:
var https = require("https");
var options = {
host: 'stream.twitter.com',
path: '/1.1/statuses/filter.json?track=bieber',
method: 'GET',
headers: {
"Authorization": "Basic " + new Buffer("username:password").toString("base64")
}
};
var request = https.request('https://stream.twitter.com/1.1/statuses/filter.json?track=query', function(response){
var body = '';
response.on("data", function(chunk){
var chunk = chunk.toString();
try {
var tweet = JSON.parse(chunk);
} catch (err) {
console.log("JSON parse error:" + err);
}
console.log(tweet.text);
});
response.on("end",function(){
console.log("Disconnected");
});
});
request.end();
我做了一個研究,並試圖調試。我最好的猜測是var tweet = JSON.parse(chunk);
可能會導致問題。第二個選項,我缺少oauth參數。
你能幫我嗎?謝謝。
編輯:
我解決了這個答案用在這裏Node.js and Twitter API 1.1
我用你的建議編輯了我的新代碼。現在錯誤是:console.log(tweet.text); TypeError:無法讀取未定義的屬性'文本'。我想這是api的oauth問題。 – ytsejam 2014-10-03 07:02:59