Create a gist now

Instantly share code, notes, and snippets.

update for actionHero 6.x
var oauth = require('oauth');
var consumerKey = "AAAAAAAAAAAAAAAAAA";
var consumerSecret = "BBBBBBBBBBBBBBBBBBBB";
var callbackURL = "http://127.0.0.1:8080/twitterOauthCatch"; // be sure to set me in the application's settings on dev.twitter.com
var oathClient = new oauth.OAuth(
"https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
consumerKey,
consumerSecret,
"1.0A",
callbackURL,
"HMAC-SHA1"
);
// http://127.0.0.1:8080/twitterOauthStart
exports.twitterOauthStart = {
name: "twitterOauthStart",
description: "I start the oauthProcess",
inputs: { required: [], optional: [] },
outputExample: {},
run:function(api, connection, next){
oathClient.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if(error){
connection.error = error.data;
api.log(error.data, "error")
next(connection, true)
}else{
api.cache.save("oauth_" + connection.id , {oauthToken: oauthToken, oauthTokenSecret: oauthTokenSecret}, 1000 * 60 * 5, function(err){
connection.response.redirectURL = "https://twitter.com/oauth/authorize?oauth_token="+oauthToken;
connection.rawConnection.responseHeaders.push(['Location', connection.response.redirectURL]);
connection.rawConnection.responseHttpCode = 302;
next(connection, true);
});
}
});
}
}
// http://127.0.0.1:8080/twitterOauthCatch
exports.twitterOauthCatch = {
name: "twitterOauthCatch",
description: "I catch the user when they come back from the oAuth provider",
inputs: { required: ["oauth_token", "oauth_verifier"], optional: [] },
outputExample: {},
run:function(api, connection, next){
api.cache.load("oauth_" + connection.id, function(err, savedOauthDetails){
if(err != null){
connection.error = error;
api.log(error.data, "error")
next(connection, true);
}else{
oathClient.getOAuthAccessToken(
savedOauthDetails.oauthToken,
savedOauthDetails.oauthTokenSecret,
connection.params.oauth_verifier,
function(error, oauth_access_token, oauth_access_token_secret, results){
if (error){
connection.error = error.data;
api.log(error.data, "error")
next(connection, true);
} else {
connection.response.twitterDetails = results;
next(connection, true);
}
}
);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment