javascript 使用Casper.js从Launchpad.net获取oauth_token
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用Casper.js从Launchpad.net获取oauth_token相关的知识,希望对你有一定的参考价值。
// 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();
});
/*
* 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();
});
以上是关于javascript 使用Casper.js从Launchpad.net获取oauth_token的主要内容,如果未能解决你的问题,请参考以下文章
javascript Devuelve la url实际
JavaScript recogida de parametros de la url mediante javascript
javascript Detectar ancho de la pantalla jQuery
JavaScript DETECTAR SI SE ENTRA LA PLAGINA POR IPHONE,ANDRIOD,JAVASCRIPT
JavaScript Establecer la misma altura para dos columnas
javascript De la lista de gente a la que sigues,te oculta solo a los que te siguen