I'm trying to track tweets via Twitter Streams API using NodeJS , but I'm facing this problem :
undefined:1
SyntaxError: Unexpected token <
at Object.parse (native)
at IncomingMessage.<anonymous> (C:\Users\ADiL-\Desktop\nodejs\twitter\tweets.js:1
7:20)
This is my code :
var https = require("https");
var option={
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(option,function(response){
var body = '';
response.on("data", function(chunk){
body += chunk;
var tweet = JSON.parse(body);
console.log("Tweet : " + tweet.text);
});
response.on("end", function(){
console.log("disconnected");
});
response.on("error", function(){
console.log("error occured");
});
});
request.end();
and when I delete the parse and show only the chunk it give that error :
<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\
n<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/statuses/filter.json?track=bieber'. Reason:
<pre> Unauthorized</pre>
</body>
</html>
so no one answered I did my effort and I found a solution , using the ntwitter module , and it works , here is the code if any one stuck in it in the future ...
first of all install the ntwitter module using :
npm install ntwitter
and this is the code :
var twitter = require("ntwitter");
var t = new twitter({
consumer_key: ' ',
consumer_secret: ' ',
access_token_key: ' ',
access_token_secret: ' '
});
var i = 0;
t.stream(
'statuses/filter',
{track : ["awesome","nodejs","other_keywords"]},
function(data){
data.on("data",function(tweets){
i++;
console.log("Tweet ",i," : ",tweets.text);
});
}
);