Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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>
share|improve this question
    
and I'm sure the username and password are correct ; PS : password contains only letters and numbers ( no characters ) – ADiL Aug 7 '14 at 10:57
up vote 0 down vote accepted

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);
    });
}
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.