我正在尝试通过 Puppeteer 中的客户端身份验证。 https://github.com/puppeteer/puppeteer/issues/1319#issuecomment-371503788中提出了一个解决方案,我正在尝试实施。
我不得不稍微修改一下解决方案 - 使用
got
而不是 request
。
运行此命令时,我发现浏览器挂起。最终因错误而退出:
(node:16672) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at Object.main (/Users/boyded01/workspace/watson/lib/debug.js:138:22)
(node:16672) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16672) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我的职能是:
async function checkPage(parsedUrl, config, number, total) {
const browser = await puppeteer.launch({
headless: false,
executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
});
const page = await browser.newPage();
// Enable Request Interception
await page.setRequestInterception(true);
page.on('request', interceptedRequest => {
// Intercept Request, pull out request options, add in client cert
const options = {
url: interceptedRequest.url(),
method: interceptedRequest.method(),
headers: interceptedRequest.headers(),
https: {
certificateAuthority: fs.readFileSync('/Users/------/Downloads/cloud-ca.pem'),
certificate: fs.readFileSync('/Users/------/certs/client.crt'),
key: fs.readFileSync('/Users/------/certs/client.key')
}
};
// Fire off the request manually
got(options, function(err, resp, body) {
// Abort interceptedRequest on error
if (err) {
console.error(`Unable to call ${options.url}`, err);
return interceptedRequest.abort('connectionrefused');
}
// Return retrieved response to interceptedRequest
interceptedRequest.respond({
status: resp.statusCode,
contentType: resp.headers['content-type'],
headers: resp.headers,
body: body
});
});
});
await page
.goto(parsedUrl.toString())
.catch(error => console.log(chalk.red(`Error loading page: ${error.message}`)));
await browser.close();
}
感谢您提供任何帮助使其发挥作用。该解决方案非常先进,我正在努力弄清楚我在这里做错了什么。
我意识到我没有正确使用got。
const resp = await got(options)
// Return retrieved response to interceptedRequest
interceptedRequest.respond({
status: resp.statusCode,
contentType: 'text/html;charset=utf-8',
headers: resp.headers,
body: resp.body
});
}
本该如此。