Reputation: 2074
I have the following twitter tokens:
var tokens = {
consumer_key: 'xxx',
consumer_secret: 'xxx',
access_token_key: 'xxx',
access_token_secret: 'xxx'
};
And the following http_options
var options = {
host: 'https://api.twitter.com',
path: '1.1/users/lookup.json?screen_name='+screen_name,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
I tried this but it failed
var options = {
host: 'https://api.twitter.com',
path: '/1.1/users/lookup.json?screen_name='+screen_name,
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
auth: {
consumer_key: 'xxx',
consumer_secret: 'xxx',
access_token_key: 'xxx',
access_token_secret: 'xxx'
}
}
In the following code:
var http_client = require('http');
var reqPost = http_client.request(options, function(res) {
res.on('data', function(d) {
buffer = buffer+d;
});
res.on('end', function() {
buffer = JSON.parse(buffer);
console.log("inside stream, BUFFER:",buffer);
});
});
reqPost.write();
reqPost.end();
If you open this link https://api.twitter.com/1.1/users/lookup.json?screen_name=testuser you might see {"errors":[{"message":"Bad Authentication data","code":215}]}
, so I don't know exactly how should I accomplish this. Can someone help me? Thank you very much.
Upvotes: 2
Views: 1526
Reputation: 715
Also been wrestling with this code. Finally got it to work with Node native https. Makes it much easier that we now have backticks. I suggest you first get it to work in Postman, click the code button, then plug in the values into this code snippet. however, you still need to perform some OAuth activities to get it to be repeatable and need an epoc timestamp. Probably easier to use and oauth module.
const http = require("https");
// prettier-ignore
const options = {
"method": "GET",
"hostname": "api.twitter.com",
"port": 443,
"path": "/1.1/collections/entries.json?id=custom-90708098097098-fake",
"headers": {
"authorization":`OAuth oauth_consumer_key="yourtwitterconsumerkey",oauth_token="yourregisteredtwittertoken", oauth_signature_method="HMAC-SHA1",oauth_timestamp="atimestame",oauth_nonce="anonceofyourchoic",oauth_version="1.0",oauth_signature="anoauthtweet"`,
"cache-control": "no-cache"
}
};
const req = http.request(options, function(res) {
let chunks = [];
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function() {
let body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
Upvotes: 1
Reputation: 1038
The way with which you are making oauth
request to access twitter API is not proper. You may need oauth
module to accomplish this task. Have a look at the README from this module repository
https://github.com/ciaranj/node-oauth
I will suggest you to use twitter module to accomplish the given task. Just look at this example.
var Twitter = require('twitter');
var client = new Twitter({
consumer_key: 'xxx',
consumer_secret: 'xxx',
access_token_key: 'xxx',
access_token_secret: 'xxx'
});
var params = {screen_name: 'nodejs'};
client.get('users/lookup.json', params, function(error, tweets, response){
if (!error) {
console.log(tweets);
}
});
Upvotes: 1