Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to connect to Twitters Streaming API over OAuth using http.get although I'm having a slight problem.

The script keeps returning unauthorised

The code I'm using follows, can anybody tell me if I'm missing something stupid or my headers are incorrect.

var https = require('https');

var options = {
  host: 'stream.twitter.com',
  path: '/1.1/statuses/filter.json?track=bieber',
  method: 'GET',
  headers: {
      authorization: '
        OAuth 
        oauth_consumer_key      =   "", 
        oauth_nonce             =   "", 
        oauth_signature         =   "", 
        oauth_signature_method  =   "HMAC-SHA1", 
        oauth_timestamp         =   "", 
        oauth_token             =   "", 
        oauth_version           =   "1.0"
      '

  }
};

var req = https.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log(chunk);
  });
});

req.on('error', function(e) {
  console.log('Oops... ' + e.message);
});

req.write('data\n');
req.write('data\n');
req.end();
share|improve this question
1  
You have an extra s in httpss:// , Also, your host is wrong, see nodejs.org/api/http.html#http_http_request_options_callback for examples – Benjamin Gruenbaum Apr 10 at 19:02
I have the request working now but I now get Unauthorised, is my OAuth headers correct? – Jacob Clark Apr 10 at 19:07
Where is all the actual header information? – Benjamin Gruenbaum Apr 10 at 19:38
I removed it for security, wasn't sure if it needed removing or not. – Jacob Clark Apr 10 at 19:40
Not needed, do not post it here, make sure it is valid – Benjamin Gruenbaum Apr 10 at 19:57
show 2 more comments

2 Answers

The problem I had here was that the OAuth request was NOT being signed, which ment the authorisation was failing.

OAuth is a complicated process and it's best to use a library or NPM module that has already been developed.

The particular NPM I used in this instance was node-oauth

share|improve this answer
1  
Have you looked at ntwitter? Takes care of everything :) – robertklep Apr 11 at 9:21
I'd like to keep the project as vanilla as possible :-)! – Jacob Clark Apr 11 at 9:23
Great, now you should accept your answer as correct so future visitors will know that it is indeed what worked for you :) – Benjamin Gruenbaum Apr 11 at 10:07
I can only do that tomorrow! Will sort it out :-) – Jacob Clark Apr 11 at 10:08

try this:

var options = {
  host: 'stream.twitter.com',
  path: '/1.1/statuses/filter.json?track=bieber',
  method: 'GET',
  auth : "YOUR_ID:YOUR_PASS"
};
var https  = require('https');
https.get(options,function(res){
   res.on("data",function(trunk){
        //YOUR CODE
   }).on("end",function(){
        //YOUR CODE
   }).on("error",function(e){
        //YOUR CODE
   });
}
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.