[英]How to authenticate using bearer token using got.js
I'm trying to switch from request.js
to got.js
.我正在尝试从
request.js
切换到got.js
。 I expect to see the got.js
implementation authenticate similarly to how the request.js
library does.我希望看到
got.js
实现以类似于request.js
库的方式进行身份验证。 But, instead, I get the following error.但是,相反,我收到以下错误。
auth no longer supported.
不再支持身份验证。 Replaced by username and password.
替换为用户名和密码。
There is no mention of bearer
tokens on the docs page.文档页面上没有提到不
bearer
令牌。
So how do I authenticate my request using bearer tokens using got.js
?那么如何使用
got.js
使用不记名令牌对我的请求进行身份验证? Or what am I doing wrong?或者我做错了什么?
const request = require('request'); const module.exports = config => { const options = { auth: { bearer: config.secret, }, }; const result = await new Promise(( resolve, reject, ) => { request.get( url, options, ( error, response, body, ) => { ...
New code: got.js, throws error
const got = require('got'); module.exports = async config => { const options = { auth: { bearer: config.secret, }, }; const result = await got(url, options); ... }
This should be worked, if I'm not wrong如果我没记错的话,这应该可行
const options = {
headers: {
'Authorization' : 'Bearer yourtoken'
}
};
worked for me !!为我工作!!
router.get('/product', (req,res)=>{
const dataStream = got.stream({
url: "http://localhost:8000/products",
method: "GET",
hooks: {
beforeRequest: [
options => {
var token= 'Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MTkzODA0NjIsImV4cCI6MTYxOTM4NDA2Mn0.JoJbRpPniGuMbwULEtts7I19QxEImvarT6AoqVuNb9w'
options.headers['Authorization'] = token;
}
]
}
});
pipeline(dataStream, res, (err) => {
if (err) {
console.log(err);
res.sendStatus(500);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.