Jhankar Mahbub
Jhankar Mahbub

Reputation: 9848

How to use oauth access token for twitter public stream

I am brand new to twitter streaming api.I want to curl.

curl https://stream.twitter.com/1.1/statuses/filter.json?track=node

How can i use oAuth access token to get public stream. My goal is to fetch public tweets about node and use node to display in a webpage. I am using ubuntu terminal.

Upvotes: 2

Views: 1572

Answers (1)

Darth
Darth

Reputation: 379

I know this question is old, but here's a solution to the curl / node portion of the question. Hopefully this is helpful to others.

Twitter setup

First, register an app with twitter at https://apps.twitter.com. This can be anything including testingAppForNode, etc. Upon completion you should have a API KEY and API SECRET under apps > yourApp > Keys and Access Tokens

Next, on the same apps.twitter.com page your API KEY / SECRET is listed, generate the Access Token / Secret. This section is just below the "Application Settings" section. Any permissions will do for this use case.

Your twitter setup should be done, but stay on the same page to test with curl.

Test with CURL

In the "Keys and Access Tokens" section on your twitter app dashboard (apps.twitter.com), you should have a "Test OAuth" button at the top right. Click that to the "OAuth Tool" (https://dev.twitter.com/oauth/tools/signature-generator/$APP_ID).

Replace the "Request URI" field with 'https://stream.twitter.com/1.1/statuses/filter.json?track=node' and click "Get OAuth Signature" button. This should generate a customized curl command that you can cut and paste into your terminal.

Connect with Node

(I found the following example here: https://github.com/ciaranj/node-oauth/wiki/Interacting-with-Twitter.)

command line:

npm install oauth

twitter.js:

var OAuth = require("oauth").OAuth,
oa = new OAuth(
    "https://api.twitter.com/oauth/request_token",
    "https://api.twitter.com/oauth/access_token",
    "YOUR-CONSUMER-KEY",
    "YOUR-CONSUMER-SECRET",
    "1.0",
    "",
    "HMAC-SHA1"
);

var access_token = "YOUR-ACCESS-TOKEN";
var access_token_secret = "YOUR-ACCESS-TOKEN-SECRET";
var request = oa.get("https://stream.twitter.com/1.1/statuses/filter.json?track=node", access_token, access_token_secret);
request.addListener('response', function(response) {
    response.setEncoding('utf8');
    response.addListener('data', function(chunk) {
        console.log(chunk);
    });
    response.addListener('end', function() {
        console.log('-- END --');
    });
});
request.end();

Run "node twitter.js" from the command line and you should see streaming feed from twitter.

Upvotes: 4

Related Questions