2

I am trying to develop a NodeJS app and i want to be able to search for tweets including a keyword here is my code:

const express = require("express");
const app = express();
var Twitter = require("twitter");
const needle = require('needle');

var client = new Twitter({
  consumer_key: "key",
  consumer_secret: "key",
  access_token_key: "key",
  access_token_secret: "key",
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

app.get("/search", (req, res) => {
    client.get('2/search/tweets', {q: 'node.js'}, function(error, tweets, response) {
        console.log(tweets);
        console.log(error)
     });
});

when i try via postman to send a request to my node app the answer is this:

{}
Error: HTTP Error: 404 Not Found
    at Request._callback (C:\Users\nikos\Desktop\node_modules\twitter\lib\twitter.js:221:9)
    at Request.self.callback (C:\Users\nikos\Desktop\node_modules\request\request.js:185:22)
    at Request.emit (node:events:527:28)
    at Request.<anonymous> (C:\Users\nikos\Desktop\node_modules\request\request.js:1154:10)
    at Request.emit (node:events:527:28)
    at IncomingMessage.<anonymous> (C:\Users\nikos\Desktop\node_modules\request\request.js:1076:12)
    at Object.onceWrapper (node:events:641:28)
    at IncomingMessage.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

If anyone can help i would greatly appreciate it since i am trying to solve how to use this api properly for the last 3 days. Thanks in advance!

  • Does the npm module you are using to access the Twitter API, support v2? From the 404 error, it looks like it is probably trying to call an API endpoint that does not exist or that it does not have access to. Also, is your Twitter app part of a Project on the Twitter Developer Portal? – Andy Piper Jun 29 '22 at 15:42
  • Well i am not sure because i just did npm i twitter as suggested in the docs and that's why i am asking i am so confused right now. Yes i registered the app and that's how i got the keys to connect to the twitter object – OutOfBounds Jun 29 '22 at 15:46
  • Well i just added the twitter v2 api and i get this error message after literally copy pasting one of their tests" – OutOfBounds Jun 29 '22 at 15:53
  • Server is running on port 3000 C:\Users\nikos\Desktop\mentionLytics\server\node_modules\twitter-api-v2\dist\client-mixins\request-handler.helper.js:103 return new types_1.ApiResponseError(errorString, { – OutOfBounds Jun 29 '22 at 15:53
  • Here is a [snippet](https://github.com/desmondmorris/node-twitter/tree/master/examples#search) I found on the GitHub repo linked to the `Twitter` npm that shows you how to search using the package – Cryptizism Sep 19 '22 at 23:01

2 Answers2

2

have you checked out the code samples in GitHub?

Ruby
  • 21
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 01 '22 at 08:57
1

Try using the official Twitter API v2 JavaScript SDK: https://github.com/twitterdev/twitter-api-typescript-sdk