似乎无法使用 Unirest 进行 cookie 身份验证请求
Cant seem to make cookie authenticated requests using Unirest
我正在尝试使用 Unirest Jar 功能来发出会话验证请求,但我似乎无法让我的名称控制台记录下来。以下是我使用的示例代码。关于如何记录我的名称控制台的任何想法?
var cheerio = require('cheerio');
var unirest = require('unirest');
var CookieJar = unirest.jar();
var twitterLogin = 'https://twitter.com/sessions';
var twitterUsername = 'TWITTERUSERNAME';
var twitterPassword = 'TWITTERPASSWORD!'
var Request = unirest.get('https://twitter.com/login').jar(CookieJar);
Request.end(function(response) {
var $ = cheerio.load(response.body);
var authToken = $('input[name="authenticity_token"]').val();
console.log(authToken)
unirest.post(twitterLogin)
.jar(CookieJar)
.followRedirect(true)
.header('Referer', 'https://twitter.com/login')
.header('content-type', 'application/x-www-form-urlencoded')
.header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36')
.field('authenticity_token', authToken)
.field('session[username_or_email]', twitterUsername)
.field('session[password]', twitterPassword)
.end(function(response) {
console.log(response.statusCode)
console.log(response.body)
unirest.get('https://twitter.com')
.jar(CookieJar)
.followRedirect(true)
.end(function(response) {
var $ = cheerio.load(response.body)
console.log($('.DashboardProfileCard-name').text());
})
})
});
来自the docs:
Request.jar(Boolean) or Request.jar(Jar)
Sets jar
, cookie container, on Request.options
. When set to true
it stores cookies for future usage.
您应该使用 true
作为参数调用 unirest.jar()
:
var CookieJar = unirest.jar(true);
我正在尝试使用 Unirest Jar 功能来发出会话验证请求,但我似乎无法让我的名称控制台记录下来。以下是我使用的示例代码。关于如何记录我的名称控制台的任何想法?
var cheerio = require('cheerio'); var unirest = require('unirest'); var CookieJar = unirest.jar(); var twitterLogin = 'https://twitter.com/sessions'; var twitterUsername = 'TWITTERUSERNAME'; var twitterPassword = 'TWITTERPASSWORD!' var Request = unirest.get('https://twitter.com/login').jar(CookieJar); Request.end(function(response) { var $ = cheerio.load(response.body); var authToken = $('input[name="authenticity_token"]').val(); console.log(authToken) unirest.post(twitterLogin) .jar(CookieJar) .followRedirect(true) .header('Referer', 'https://twitter.com/login') .header('content-type', 'application/x-www-form-urlencoded') .header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36') .field('authenticity_token', authToken) .field('session[username_or_email]', twitterUsername) .field('session[password]', twitterPassword) .end(function(response) { console.log(response.statusCode) console.log(response.body) unirest.get('https://twitter.com') .jar(CookieJar) .followRedirect(true) .end(function(response) { var $ = cheerio.load(response.body) console.log($('.DashboardProfileCard-name').text()); }) }) });
来自the docs:
Request.jar(Boolean) or Request.jar(Jar)
Sets
jar
, cookie container, onRequest.options
. When set totrue
it stores cookies for future usage.
您应该使用 true
作为参数调用 unirest.jar()
:
var CookieJar = unirest.jar(true);