I am trying to connect to public stream endpoint of twitter api.
When i ran example parameters i got from twitter api test tool in nodejs i get 401 unauthorized
error below you can see my nodejs example request
'use strict';
const https = require('https');
const qs = require('querystring');
const obj = {
method: "POST",
headers: {
Authorization: 'OAuth oauth_consumer_key="pXZ8xSWvjZjE4UaNntVsCwWPg", oauth_nonce="776f1a4ec0c3ed9ecbfb68aa0f7e2324", oauth_signature="jk8e84V0OcgcepkL7F%2BXt2fAy8o%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1463258508", oauth_token="416282416-vFTMVJCFiRW8G6nwoFhH3OBqrJsKDbIJ93sbOm2a", oauth_version="1.0"'
},
hostname: "stream.twitter.com",
path: "/1.1/statuses/filter.json"
}
https.request(obj,res => {
res.on("data", d => {
console.log(d.toString());
});
}).end(qs.stringify({track:"javascript"}))
But if i send this request with curl it perfectly works here is my example curl request
curl --request 'POST' 'https://stream.twitter.com/1.1/statuses/filter.json' --data 'track=javascript' --header 'Authorization: OAuth oauth_consumer_key="pXZ8xSWvjZjE4UaNntVsCwWPg", oauth_nonce="776f1a4ec0c3ed9ecbfb68aa0f7e2324", oauth_signature="jk8e84V0OcgcepkL7F%2BXt2fAy8o%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1463258508", oauth_token="416282416-vFTMVJCFiRW8G6nwoFhH3OBqrJsKDbIJ93sbOm2a", oauth_version="1.0"' --verbose
what am i doing wrong ?they are tottaly identical requests.
2
Answers
You can’t hardcode the timestamp in the OAuth request. If the code you are presenting is the one you are using then this could be the cause of the problem. I recomend you use the oauth package to generate the Authorization header.
Same thing just happened to me, and I ended up using axios library to resolve this.
Not sure what is the difference between axios and the built in https request, but the fact is that using axios worked where the https request failed. (and curl worked as well.)