Skip to content

Instantly share code, notes, and snippets.

@Echooff3
Last active August 29, 2015 14:02
Show Gist options
  • Save Echooff3/69e6cdb7a6800f4f61b3 to your computer and use it in GitHub Desktop.
Save Echooff3/69e6cdb7a6800f4f61b3 to your computer and use it in GitHub Desktop.
Local Twitter Auth
var open = require('open');
var url = require('url');
var http = require('http');
var twitterAPI = require('node-twitter-api');
var Q = require('q');
var authObject = (function () {
var deferred = Q.defer();
var twitter = {};
var token = {};
var options = {
host: 'twitter.com',
path: '/oauth/authenticate?oauth_token='
};
var httpcallback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
open(this.req.res.headers.location);
});
}
var svr = http.createServer(function(request, response) {
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
if(query != undefined) {
token.oauth_token = query.oauth_token;
token.oauth_verifier = query.oauth_verifier;
getAccessToken();
}
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Request Complete');
response.end();
}).listen(8888);
function auth(consumerKey, consumerSecret) {
twitter = new twitterAPI({
consumerKey : consumerKey,
consumerSecret : consumerSecret,
callback : 'http://127.0.0.1:8888/auth'
});
twitter.getRequestToken(function(error, requestToken, requestTokenSecret, results){
if (error) {
console.log('auth',"Error getting OAuth request token : " + error);
deferred.reject(new Error("Status code was " + error));
} else {
//store token and tokenSecret somewhere, you'll need them later; redirect user
token.requestToken = requestToken;
token.requestTokenSecret = requestTokenSecret;
token.stepOneResults = results;
deferred.notify("Request Tokens successful. Awaiting callback.");
options.path += requestToken;
http.request(options, httpcallback).end();
}
});
}
var reqcount = 3;
function getAccessToken() {
twitter.getAccessToken(token.requestToken, token.requestTokenSecret, token.oauth_verifier, function(error, accessToken, accessTokenSecret, results) {
if (error) {
//3 bad requests and kick out
deferred.notify('Bad request.' + reqcount-- + ' tries left.' );
if(reqcount == 0) {
svr.timeout = 10;
svr.close();
deferred.reject(new Error('Too many bad requests.'));
}
} else {
//store accessToken and accessTokenSecret somewhere (associated to the user)
//Step 4: Verify Credentials belongs here
token.accessToken = accessToken;
token.accessTokenSecret = accessTokenSecret;
svr.timeout = 10;
svr.close();
deferred.resolve(token,this);
}
});
}
return {
twitter : twitter,
token : token,
options : options,
httpcallback : httpcallback,
svr : svr,
auth : auth,
getAccessToken : getAccessToken,
deferred : deferred
}
}
)();
exports.deferred = authObject.deferred;
exports.token = authObject.token;
exports.auth = function (consumerKey, consumerSecret) {
authObject.auth(consumerKey, consumerSecret);
return authObject.deferred.promise;
}
@jeffreytgilbert
Copy link

If you would like to extract the params from the query string, you can use the require('querystring').parse function, or pass true as the second argument to require('url').parse. Example:

node> require('url').parse('/status?name=ryan', true)
{ href: '/status?name=ryan',
search: '?name=ryan',
query: { name: 'ryan' },
pathname: '/status' }

@jeffreytgilbert
Copy link

If(query.xyz && query.zyx){...}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment