Created
July 17, 2013 05:58
-
-
Save fourdollars/6018003 to your computer and use it in GitHub Desktop.
A simple NodeJS server made by ExpressJS and node-request that uses https://help.launchpad.net/API/SigningRequests to get personal information from https://launchpad.net/ .
This file contains 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
#!/usr/bin/env lsc | |
require! express | |
require! request | |
app = express! | |
oauth_consumer_key = 'just+testing' | |
var oauth_token | |
var oauth_token_secret | |
app.get \/oauth, ! (req, res) -> | |
(err, res2, body) <-! request.post \https://launchpad.net/+request-token, do | |
form: do | |
oauth_consumer_key: oauth_consumer_key | |
oauth_signature_method: \PLAINTEXT | |
oauth_signature: \& | |
console.log err, res2 if err | |
obj = {[k, v] for _ in body.split \& when [k, v] = _.split \=} | |
oauth_token := obj.oauth_token | |
oauth_token_secret := obj.oauth_token_secret | |
console.log oauth_token, oauth_token_secret | |
res.redirect "https://launchpad.net/+authorize-token?oauth_token=#oauth_token&oauth_callback=http://localhost:3000/oauth-callback" | |
app.get \/oauth-callback, ! (req, res) -> | |
(err, res2, body) <-! request.post \https://launchpad.net/+access-token, do | |
form: do | |
oauth_consumer_key: oauth_consumer_key | |
oauth_token: oauth_token | |
oauth_signature_method: \PLAINTEXT | |
oauth_signature: "&#oauth_token_secret" | |
console.log err, res2 if err | |
obj = {[k, v] for _ in body.split \& when [k, v] = _.split \=} | |
oauth_token := obj.oauth_token | |
oauth_token_secret := obj.oauth_token_secret | |
console.log oauth_token, oauth_token_secret | |
res.redirect \/ | |
lp_request = ! (url, callback) -> | |
(err, res, body) <-! request do | |
url: url | |
json: true | |
followRedirect: false | |
headers: do | |
'Accept': \application/json | |
'Authorization': """OAuth realm="https://api.launchpad.net/", | |
oauth_consumer_key="#oauth_consumer_key", | |
oauth_token="#oauth_token", | |
oauth_signature_method="PLAINTEXT", | |
oauth_signature="&#oauth_token_secret", | |
oauth_timestamp="#{+new Date / 1000}", | |
oauth_nonce="#{Math.floor(Math.random! * +new Date)}", | |
oauth_version="1.0" | |
""" | |
return callback err, res if err | |
if res.statusCode == 303 | |
lp_request res.headers.location, callback | |
else | |
callback err, res, body | |
app.get \/, ! (req, res) -> | |
unless oauth_token? and oauth_token_secret? | |
res.redirect \/oauth | |
else | |
(err, res2, body) <-! lp_request \https://api.launchpad.net/1.0/people/+me | |
console.log err, res2 if err | |
if body == "Unknown consumer (#oauth_consumer_key)." | |
res.redirect \/oauth | |
else | |
res.write JSON.stringify(body) | |
res.end! | |
app.listen 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment