How to get twitter api access using http request from browser - node.js

Is there any way to get the data from twitter api for any location?
var sanFrancisco = ['79.86','12.62','80.28','13.21']
var stream = T.stream('statuses/filter', { locations: sanFrancisco })
stream.on('tweet', function (tweet) {
console.log(tweet)
It gives an error:
Error: Bad Twitter streaming request: 401
at Object.exports.makeTwitError (/home/file_upload/node_modules/twit/lib/helpers.js:74:13)
at IncomingMessage. (/home/file_upload/node_modules/twit/lib/streaming-api-connection.js:95:29)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)

I assume you followed the usage guide here https://github.com/ttezel/twit#usage and created the twit object with suitable OAuth keys?
Another possibility for this error is if your clock is out of sync, the twitter endpoint will reject requests if the time is out by a few minutes. Check your computers time is correct.

Related

InternalOAuthError discord oauth2

I am trying to log my users in using discord oauth2
I noticed, when clicking the authorize button multiple times, I am getting this error.
InternalOAuthError: Failed to fetch user's guilds
at S:\project\node_modules\passport-discord\lib\strategy.js:108:32
at passBackControl (S:\project\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.<anonymous> (S:\project\node_modules\oauth\lib\oauth2.js:157:7)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
After that I also get this error, but idk if that is important:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I am using passport, passport-discord and express-session
Yes this is because of the rate limit that discord has. I do not know any solution for this beside changing you passport to keep track of the amount of request made to discord.

GET request using node/request throwing unexpected "read ECONNRESET"

I am getting strange behavior using the request library in node when hitting certain sites.
for a quick context (that has nothing to do with the technical issue): my goal on this project was to read the NASDAQ dividend history for a number of stocks. The histories can be quite long and I'm certainly not going to sit here and read 100 of them manually. An example of one of these pages: https://www.nasdaq.com/symbol/msft/dividend-history
On to the technical troubles... I am using node v11.10.0 with the request module to call the page - the simplest possible thing. I have used this library to do other similar tasks and have never had an issue. Here was my very simple script:
var request = require("request");
var options = { method: 'GET',
url: 'https://www.nasdaq.com/symbol/msft/dividend-history',
headers:
{ 'Postman-Token': 'e4e9749e-d73e-44b9-994f-9d0522654fb0',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
Save file as index.js, and run
node index.js
and that should get the page... except I'm getting an error. The error is:
Error: Error: read ECONNRESET
at Request._callback (<localpath>\index.js:8:24)
at self.callback (<localpath>\node_modules\request\request.js:185:22)
at Request.emit (events.js:197:13)
at Request.onRequestError (<localpath>\node_modules\request\request.js:881:8)
at ClientRequest.emit (events.js:197:13)
at TLSSocket.socketErrorListener (_http_client.js:397:9)
at TLSSocket.emit (events.js:197:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at processTicksAndRejections (internal/process/next_tick.js:76:17)
So, thinking that I maybe had something wrong, I tried the same request in postman to validate. But Postman had no trouble doing a get request against the same URL above. I thought perhaps there was a certificate issue with the https stuff, but I toggled certificate validation on and off in postman, and it worked regardless of the setting.
Additional note here - my "very simple script" is actually what I pulled from the Postman "code" tab for a Node/Request code block, so I am really baffled that Postman works but Postman's own node codeblock does not.
At this point I don't even care about what I was trying to do originally... I'm just trying to figure out why I am getting different behavior on two seemingly equivalent ways to make a GET request.
Does anyone have insight on what postman has that node is missing in this case?

Node.js Request() failing on pages of one specific domain

My customer is all of a sudden experiencing problems with a HTML scraper job made with Node.js. I have circled in on the cause, and found that it's located in the Request module. That made me write a small test application, which solely gets the HTML of the given URL via the Request module. Like this:
var request = require('request');
request('https://www.politi.dk/da/ompolitiet/jobipolitiet/ledige_stillinger/ledigestillinger', function(err, res, body){
if(err){
console.log(err);
} else {
console.log('statusCode:', res.statusCode);
console.log('statusMessage:', res.statusMessage);
}
});
The above example does not work though, as I am getting the following error when running the application:
{ Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1137:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
code: 'ECONNRESET',
path: null,
host: 'www.politi.dk',
port: 443,
localAddress: undefined }
However if I change the URL to any other URL it works and I get the following:
statusCode: 200
statusMessage: OK
I have tried passing other URL's on the politi.dk domain, which doesn't work either. Therefore I can conclude that there's a problem with this domain, when requesting pages via the Request module. The strange thing is just, that it worked up until recently. What can cause this problem? Can some changes in settings be made to the server of politi.dk, that is causing this now? I find it hard to find anything helpful on Google. I found the nodejs-what-does-socket-hang-up-actually-mean thread here on SO, which is the exact same problem. But the answers doesn't help me much.
Anyone?

Using node.js request with lets encrypt "unable to verify the first certificate" error

I am using node-slack to use the Mattermost incoming URL Api for a self hosted mattermost. This mattermost is secured by lets-encrypt.
My node code is working with the slack api, and i have testet the send data with Postman (manual post tool) against the MM server.
In both cases it is working.
But with node i receive a unable to verify the first certificate error.
In "https://nodejs.org/api/https.html" I can see, that node.js is supporting a "well known" set of root CAs.
To simplify the question, I have written this small peace of code:
let request = require('request');
request("https:--letsencryptsecuredsite",function(err,body){
console.log(err);
console.log(body);
});
This results in
{ Error: unable to verify the first certificate
at Error (native)
at TLSSocket.<anonymous> (_tls_wrap.js:1079:38)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket._finishInit (_tls_wrap.js:603:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:433:38) code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' }
What do i have to use an API on a lets encrypt secured server ?
I am using node version 6.9.2.

Heroku and Chaining Nested HTTP Requests (Alamofire/NodeJS)

I am attempting to nest two get requests inside of each other
Alamofire.request(.GET, "serverAddress")
.responseJSON { response in
if let JSON = response.result.value {
//populate parameters with the expected args
var parameters:[String:[String]] = [String:[String]]()
//Request2
self.queryFriendsList(parameters)
}
Here is the nested request within a different method...
func queryFriendsList(parameters:[String:[String]]) {
Alamofire.request(.GET, "serverAddress", parameters : parameters, encoding: .JSON)
.responseJSON {
response in
if let res = response.result.value {
print(res)
}
}
}
When I check my logs on Heroku I see:
heroku[router]: sock=client at=warning code=H27 desc="Client Request Interrupted" method=GET path="/query_friends_list/"
The log associated with the app reports:
Error: request aborted
at IncomingMessage.onAborted (/app/node_modules/raw- body/index.js:269:10)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at abortIncoming (_http_server.js:281:11)
at emitOne (events.js:82:20)
at Socket.serverSocketCloseListener (_http_server.js:294:5)
at Socket.emit (events.js:169:7)
The second request is dependent upon the first succeeding.
I am at a loss as to how to debug this one. I am using the free version of Heroku, so I wasn't able to determine from the docs if multiple requests would be an issue ?
The issue with this bug had to do with me not understanding where to look for the error...
sock=client
Denoted that the issue was associated with the client side. Initially I had specified a get request for my http request to heroku. I specified parameters to be encoded with JSON but I realized I cannot send a payload encoded with JSON on a get request.
This was really the root issue at hand. After switching my route to a post request and updating the calls in my swift project, I was able to make the proper query against my DB!

Resources