Created
September 24, 2012 19:23
-
-
Save nolastan/3777787 to your computer and use it in GitHub Desktop.
node.js taskrabbit api example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var action = {}; | |
///////////////////////////////////////////////////////////////////// | |
// metadata | |
action.name = "oauth-taskrabbit-callback"; | |
action.description = ""; | |
action.inputs = { | |
"required" : ["code"], | |
"optional" : [] | |
}; | |
action.outputExample = { | |
randomNumber: 123 | |
} | |
///////////////////////////////////////////////////////////////////// | |
// functional | |
action.run = function(api, connection, next){ | |
var browser_fingerprint = api.session.uid(api, connection); | |
api.models.user.find({where: {browser_fingerprint: browser_fingerprint}}).on('success', function(user){ | |
if(user == null){ | |
connection.error = "User not found"; | |
next(connection, true); | |
}else if(user.taskrabbit_access_token != null){ | |
connection.error = "User already has token"; | |
next(connection, true); | |
}else{ | |
var oauth_code = connection.params.code; | |
var url = 'https://' + api.configData.taskrabbit.taskrabbitServer + "/api/oauth/token/?"; | |
url += "grant_type=authorization_code" | |
url += "&code=" + oauth_code | |
url += "&client_secret=" + api.configData.taskrabbit.consumerSecret | |
url += "&client_id=" + api.configData.taskrabbit.clientid; | |
url += "&redirect_uri=" + api.this_site + "/public/oauth_callback_taskrabbit.html"; | |
console.log(url) | |
api.request.post(url, function (error, response, body) { | |
var data = JSON.parse(body); | |
user.taskrabbit_access_token = data.access_token; | |
user.save().on("success", function(){ | |
next(connection, true); | |
}).on("failure", function(error){ | |
connection.error = error; | |
next(connection, error); | |
}); | |
}); | |
} | |
}); | |
}; | |
///////////////////////////////////////////////////////////////////// | |
// exports | |
exports.action = action; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment