Resolving 401 with Streaming API and OAuth


#1

I have a simple node.js app that connects to the streaming API and filters responses with some track key words.

The original version was based on the 1.0 API and I understand that Basic Auth has been deprecated/disabled and that OAuth is now required.

I’ve updated the code to include the OAuth Authorization header (generated on https://dev.twitter.com/apps/myappid/oauth but I must be doing something wrong because I get a 401, Not Authorized.

I’ve ensured my clock is correct, triple checked my keys and even reset them just to make sure.

I know there are node modules that do this for you like ntwitter, node-oauth twit, etc. but I would like to avoid using a module if nothing else than to learn the right way to put together the request. I realize that the header has a shelf life, so not sure if this is the problem. I am also curious if anything else needs to be done to obtain a valid OAuth signature (my assumption is that the oauth tool on dev.twitter.com takes care of this for you and puts the result in the oauth_signature parameter).

Here is the code:

var ws = require('websocket.io'), server = ws.listen(88), http = require('https');

var sockets =[];

server.on(‘connection’, function (socket) {

console.log('Connected to client');

sockets.push(socket);

var options = {
    host: 'stream.twitter.com',
    port: 443,
    path: "/1/statuses/filter.json?track=NY%20twitpic&track=NY%20photo",
    method: 'GET',
    headers:{
        "Authorization": 'OAuth oauth_consumer_key="", oauth_nonce="", oauth_signature="", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1390855545", oauth_token="", oauth_version="1.0"'
    }

};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));

    res.on('data', function (chunk) {

        console.log('BODY: ' + chunk);
        var s;
        for(i=0;i<sockets.length;i++)
        {
            sockets[i].send(chunk);
        }
        //socket..send(chunk);
    });
});

NOTE: I’ve removed the values of each OAuth parameter leaving an empty string ("").

I would appreciate any help you can provide as searches on the web all point to modules to solve the problem (I haven’t cracked open their source yet, because I have to imagine there is a simple way to accomplish this).

Thanks,


#2

I’d be curious to know if you got this working? I am hitting a similar 401 and am not sure why. Time and keys are correct. Any ideas?