Last active
August 15, 2018 13:30
-
-
Save penk/6265347 to your computer and use it in GitHub Desktop.
Obtain oauth_token from Launchpad.net using Casper.js
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
/* | |
* oauth.js - Obtain oauth_token from Launchpad.net using Casper.js | |
* Copyright (c) 2013, Ping-Hsun (penk) Chen <penk.chen@canonical.com> | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* Usage: casperjs oauth.js <OTP> | |
*/ | |
var casper = require('casper').create({ | |
waitTimeout: 50000, | |
verbose: true, | |
logLevel: "debug", | |
}), | |
credentials = { | |
email: 'YOUR_LAUNCHPAD_EMAIL', | |
password: 'YOUR_LAUNCHPAD_PASSWORD', | |
}; | |
casper.start(); | |
casper.then(function() { | |
this.open('https://launchpad.net/+request-token', { | |
method: 'post', | |
data: { | |
"oauth_consumer_key": "oauthjs", | |
"oauth_signature_method": "PLAINTEXT", | |
"oauth_signature": "&" | |
} | |
}); | |
}); | |
casper.then(function() { | |
var objs = this.getPageContent().split('&'); | |
for (var i = 0; i < objs.length; i++) { | |
var tmp = objs[i].split('='); | |
if (tmp[0] == 'oauth_token') | |
credentials.oauth_token = tmp[1]; | |
if (tmp[0] == 'oauth_token_secret') | |
credentials.oauth_token_secret = tmp[1]; | |
} | |
}); | |
casper.then(function() { | |
casper.thenOpen("https://launchpad.net/+authorize-token?oauth_token="+credentials.oauth_token, function() { | |
this.fill('form#login-form', { | |
'email': credentials.email, | |
'password': credentials.password, | |
}, true); | |
}); | |
}); | |
casper.waitForUrl(/two_factor_auth/, function() { | |
casper.fill('form#login-form', { | |
'oath_token': casper.cli.args[0] | |
}, true); | |
casper.waitForSelector('button[name$="yes"]', function() { | |
casper.thenClick('button[name$="yes"]'); | |
}); | |
casper.waitForSelector('input[name$="field.actions.READ_PRIVATE"]', function(){ | |
casper.thenClick('input[name$="field.actions.READ_PRIVATE"]'); | |
}); | |
}); | |
casper.then(function(){ | |
casper.echo(casper.getPageContent(), 'INFO'); | |
casper.thenOpen('https://launchpad.net/+access-token', { | |
method: 'post', | |
data: { | |
"oauth_token": credentials.oauth_token, | |
"oauth_consumer_key": "oauthjs", | |
"oauth_signature_method": "PLAINTEXT", | |
"oauth_signature": '&'+credentials.oauth_token_secret | |
} | |
}); | |
}); | |
casper.then(function(){ | |
var objs = casper.getPageContent().split('&'); | |
for (var i = 0; i < objs.length; i++) { | |
var tmp = objs[i].split('='); | |
if (tmp[0] == 'oauth_token') | |
credentials.oauth_token = tmp[1]; | |
if (tmp[0] == 'oauth_token_secret') | |
credentials.oauth_token_secret = tmp[1]; | |
} | |
casper.echo('credentials: ', 'INFO'); | |
require('utils').dump(credentials); | |
}); | |
casper.run(function() { | |
this.exit(); | |
}); |
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
// Usage: casperjs test_oauth.js <URL> | |
var casper = require('casper').create({ | |
waitTimeout: 50000, | |
verbose: true, | |
logLevel: "debug", | |
}), | |
settings = { | |
consumer_key: 'oauthjs', | |
oauth_token: 'YOUR_OAUTH_TOKEN', | |
oauth_signature: 'YOUR_OAUTH_SIGNATURE', | |
}; | |
casper.start(); | |
casper.then(function() { | |
casper.open(casper.cli.args[0], { | |
headers: { | |
'Host': 'api.launchpad.net', | |
'Accept': 'application/json', | |
'Authorization': 'OAuth realm="https://api.launchpad.net/",'+ | |
'oauth_consumer_key="'+settings.consumer_key+'",'+ | |
'oauth_token="'+settings.oauth_token+'",'+ | |
'oauth_signature_method="PLAINTEXT",'+ | |
'oauth_signature="&'+settings.oauth_signature + '",'+ | |
'oauth_timestamp="'+ (new Date / 1000) + '",'+ | |
'oauth_nonce="'+Math.floor(Math.random() * 1000000000)+'",'+ | |
'oauth_version="1.0"' | |
} | |
}); | |
}); | |
casper.then(function(){ | |
casper.echo(casper.getPageContent(), 'INFO'); | |
}); | |
casper.run(function() { | |
this.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment