I’m trying to request the tokens for the oauth connection, but i get error code 32, that says that i could not authenticate.
here is my code in node.js:
var express = require('express');
var router = express.Router();
var httpReq = require('request');
var hmac = require('hmacsha1');
var consumer_key = 'Viwb1iVT82TovjTFcmvBTgi3Awq';
var consumer_secret_key = '0V871iv3K35PErClD0E3vWcjxkPNIHpy6yDh8xfCAwOXHFUPeH';
/* GET Twitter insights. */
router.get('/', function(req, res, next){
var address = req.connection.localAddress.replace('::ffff:', 'http://');
var port = req.connection.localPort;
var redirect_uri = address + ':' + port + '/twitter';
redirect_uri = encodeURIComponent(redirect_uri);
var nonce = 'K7ny27JTpKVsTgdyLdDfmQQWVLERj2zAK5BslRsqyw';
var time = Math.floor(new Date() / 1000);
var target_url = encodeURIComponent('https://api.twitter.com/oauth/request_token');
var args = 'oauth_callback=' + redirect_uri + '&oauth_consumer_key=' + consumer_key + '&oauth_nonce=' + nonce + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + time + '&oauth_version=1.0';
console.log(args + '\n');
args = encodeURIComponent(args);
var toSign = 'POST&' + target_url + '&' + args;
var key = consumer_secret_key + '&';
console.log(key + '\n');
var signature = hmac(key, toSign);
console.log(signature + '\n');
signature = encodeURIComponent(signature);
var authHeaders = 'OAuth oauth_nonce="' + nonce + '", oauth_callback="' + redirect_uri + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + time + '", oauth_consumer_key="' + consumer_key + '", oauth_signature="' + signature + '", oauth_version="1.0"';
console.log(authHeaders + '\n');
console.log(toSign + '\n');
var options = {
headers: {'Accept' : '*/*', 'Connection' : 'close', 'Content-Type' : 'application/x-www-form-urlencoded', 'Host' : 'api.twitter.com', 'Authorization' : authHeaders},
url: 'https://api.twitter.com/oauth/request_token',
method: 'POST'
};
httpReq(options, function(err, response, body){
if(!err && response.statusCode == 200){
console.log(body);
res.send('tutto ok');
}
else{
console.log(body);
res.send('error');
}
});
});
module.exports = router;