semmypurewal / LearningWebAppDev

Examples from the book "Learning Web Application Development".
http://learningwebappdev.com
268 stars 356 forks source link

Page 197: Status error 401 When running tweet counter. (chapter 6, the server) #86

Open Ivanislost opened 5 years ago

Ivanislost commented 5 years ago

I've come across a connection issue I have been unable to resolve. I've tried various different methods which I either found online or thought of myself. I will first show the Error code from my terminal, after that I will mention what I have tried and what my setup is.

The error at running twitter.js (I've named it twitter.js in stead of tweet_counter.js)

Error: Status Code: 401
    at Request.<anonymous> (/home/vagrant/app/twitterapi/node_modules/twitter/lib/twitter.js:277:28)
    at emitOne (events.js:77:13)
    at Request.emit (events.js:169:7)
    at Request.onRequestResponse (/home/vagrant/app/twitterapi/node_modules/request/request.js:1066:10)
    at emitOne (events.js:77:13)
    at ClientRequest.emit (events.js:169:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:414:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)
    at TLSSocket.socketOnData (_http_client.js:304:20)
    at emitOne (events.js:77:13)

My js code looks exactly like the example in the book on page 197. I have learned that the status error 401 is thrown when there are authorization issues, OR at other non-described moments (according to Twitter API).

Differences in setup: -I have installed node packages locally in the twitter folder because I had errors installing Express. (I first ran npm init, followed by npm install express) -I run Ubuntu 16.04 -I run virtual box 5.2 (instead of 6)

A couple of methods I have tried: -Using different twitter API's instead of ntwitter (such as 'twitter' and 'twit') -Creating an object variable containing the authorization codes and used that to initialize my twitter -Solely ran the twitter creation line (without the stream), which gave no errors. -I have double triple quadruple checkefd the auth codes and its var names. -Used a different statement instead of statuses/filter and track : .

Unfortunately I have broken my head trying to solve this for a couple of hours, I enjoyed trying to figure out what the issue is, but I will continue without using this example nor the subsequent practice exercises using this example. Never the less I would like to see if anyone could find a solution. I would love to experiment using the twitter API

Thank You!

My js and json file are followed.

var twitter = require("twitter"),
    credentials = require("./credentials.json"),
    client;

var credentialObject = {
"consumer_key": "xx",
"consumer_secret": "xx",
"access_token_key": "xx",
"access_token_secret": "xx"
};

console.log(credentials);

//setting up our twitter object
client = new twitter(credentials);

//setting up our twitter stream with three parameters, seperated by commas
client.stream(
    //the first parameter is a string
    "statuses/filter",

    //second parameter an object containing an array
    {"track": ["Awesome", "cool", "rad", "gnarly", "groovy"]},

    //the third parameter is our callback for when the stream is created
    function(stream){
        stream.on("data", function(tweet){
            console.log(tweet.text);
        });

         stream.on('error', function(error) {
                throw error;
        });
    }
);

//Method two I tried
var params = {"track": ["Awesome", "cool", "rad", "gnarly", "groovy"]};
client.get('statuses/filter', params, function(error, tweets, response) {
  if (!error) {
    console.log(tweets);
  }
});

credentials.json

{
"consumer_key": "xx",
"consumer_secret": "xx",
"access_token_key": "xx-xx",
"access_token_secret": "xx"
}
dd301 commented 2 years ago

I can't run the tweet counter on the terminal either. I'm getting a different error. I've double checked my code and installed express on the directory. I'm also getting a message that ntwitter is unmaintained, so maybe that's the issue.