Skip to content

Instantly share code, notes, and snippets.

@sulmanen
Created July 5, 2013 13:45
Show Gist options
  • Save sulmanen/5934616 to your computer and use it in GitHub Desktop.
Save sulmanen/5934616 to your computer and use it in GitHub Desktop.
Node twitter search services
var request = require('request');
var restify = require('restify');
var consumer_key = YOUR_KEY;
var consumer_secret = YOUR_SECRET;
var enc_secret = new Buffer(consumer_key + ':' + consumer_secret).toString('base64');
var bearer = {}
var oauthOptions = {
url: 'https://api.twitter.com/oauth2/token',
headers: {'Authorization': 'Basic ' + enc_secret, 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: 'grant_type=client_credentials'
};
var queryOptions = {
url: 'https://api.twitter.com/1.1/search/tweets.json'
}
request.post(oauthOptions, function(e, r, body) {
bearer = JSON.parse(body).access_token;
console.log(bearer);
});
var server = restify.createServer();
server.use(
function crossOrigin(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET,OPTIONS");
return next();
}
);
server.use(restify.CORS());
server.use(restify.fullResponse());
server.opts(/\.*/, function (req, res, next) {
res.send(200);
next();
});
server.get('/querty/:q', function create(req, res, next) {
queryOptions.headers = {'Authorization': 'Bearer ' + bearer};
console.log('Your query is ' + req.params.q);
queryOptions.qs = {
q: req.params.q
};
request.get(queryOptions, function(e, r, body) {
console.log(body);
res.send(200, JSON.parse(body));
next();
});
});
server.listen(8888);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment