Created
October 16, 2017 07:24
-
-
Save chenzhenjia/cd1008de3c53828976d1e2af3ea849d5 to your computer and use it in GitHub Desktop.
微博登录
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
const rp = require('request-promise'); | |
const RSAKey = require('./WBPassword'); | |
const Promise = require('promise'); | |
// rp.debug = true; | |
/** | |
* 微博的预先登录,用来获取微博的公钥 | |
* @param username 用户名 | |
*/ | |
function preLogin(username) { | |
const su = new Buffer(username.replace('@', '%40')).toString('base64'); | |
return rp({ | |
url: 'https://login.sina.com.cn/sso/prelogin.php?client=ssologin.js(v1.4.18)', method: 'get', | |
qs: { | |
entry: 'homepage', | |
su: su, | |
rsakt: 'mod', | |
checkpin: 1 | |
}, | |
json: true | |
}) | |
.then(function (resp) { | |
resp.su = su; | |
return resp; | |
}) | |
} | |
const loginWB = function () { | |
// 用来保存 cookie 的,登录成功或从 jar 里面获取 cookie | |
this.jar = rp.jar(); | |
this.http = rp.defaults({ jar: this.jar }); | |
} | |
/** | |
* 登录完成后获取用户信息和 cookie | |
*/ | |
loginWB.prototype.ticket = function (ticket) { | |
return this.http({ uri: ticket, resolveWithFullResponse: true }); | |
}; | |
/** | |
* 登录微博 | |
* @param preLoginResult 预先登录的返回值 | |
* @param password 密码 | |
*/ | |
loginWB.prototype.doLogin = function (preLoginResult, password) { | |
const key = new RSAKey(); | |
key.setPublic(preLoginResult.pubkey, '10001'); | |
const options = { | |
json: true, | |
url: 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)&_=1484102991785', | |
method: 'POST', | |
gzip: true, | |
headers: { | |
'Host': 'login.sina.com.cn', | |
'Origin': 'http://www.sina.com.cn', | |
referer: 'http://login.sina.com.cn/signup/signin.php?entry=sso' | |
}, | |
form: { | |
entry: 'weibo', | |
gateway: 1, | |
from: '', | |
savestate: 30, | |
useticket: 0, | |
pagerefer: '', | |
pcid: preLoginResult.pcid, | |
door: captcha ? captcha.result : '', | |
vsnf: 1, | |
su: preLoginResult.su, | |
service: 'miniblog', | |
servertime: preLoginResult.servertime, | |
nonce: preLoginResult.nonce, | |
pwencode: 'rsa2', | |
rsakv: preLoginResult.rsakv, | |
sp: key.encrypt([preLoginResult.servertime, preLoginResult.nonce].join("\t") + "\n" + password), | |
sr: '1280*800', | |
encoding: 'UTF-8', | |
prelt: 46, | |
cdult: 3, | |
returntype: 'TEXT' | |
} | |
}; | |
const self = this; | |
return this.http(options) | |
.then(function (resp) { | |
const code = resp.retcode; | |
if (code !== '0') { | |
if (code === '4049') { | |
return Promise.reject("需要验证码"); | |
} | |
// 101 用户名错误,2070 验证码不正确 | |
if (code === '2070') { | |
return Promise.reject("验证码错误"); | |
} | |
return Promise.reject(resp.reason); | |
} | |
return resp; | |
}) | |
.then(function (resp) { | |
const urlList = resp.crossDomainUrlList; | |
if (urlList.length > 0) { | |
return self.ticket(urlList[0]); | |
} | |
return Promise.reject("登录失败"); | |
}) | |
} | |
loginWB.prototype.login = function (username, password) { | |
const self = this; | |
return preLogin(username) | |
.then(function (res) { | |
return self.doLogin(res, password) | |
}) | |
.then(function (res) { | |
var body = res.body; | |
body = body.replace(/\(|\);/g, ''); | |
body = JSON.parse(body); | |
// console.log('body', self.jar); | |
var cookies = self.jar.getCookieString('http://weibo.com'); | |
cookies += '; ' + self.jar.getCookieString('http://sina.com.cn'); | |
console.log('cookies', cookies); | |
body.cookies = cookies; | |
return body | |
}); | |
} | |
new loginWB().login('18574395060', 'cfQJrnJEpDC84kG') | |
.done() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment