![](/img/trans.png)
[英]Where should I pass the Bearer Authentication Token in a React project?
[英]Bearer Authentication in React
如何在 React 中使用带有 superagent 的承载身份验证? 我不确定语法,也找不到示例。
我现在应该做什么
showTransactionList = () => { superagent .post('http://193.124.114.46:3001/api/protected/transactions') .set({'Authorization': 'Bearer ' + this.state.id_token}) .accept('application/json') .then(res => { const posts = JSON.stringify(res.body); console.log(posts); }) .catch((err) => { console.log(err); throw err; }); }
谢谢!
设置标题的方式是提供标题项目名称和值,请尝试:
showTransactionList = () => {
superagent
.post('http://193.124.114.46:3001/api/protected/transactions')
.set('Authorization', 'Bearer ' + this.state.id_token)
.accept('application/json')
.then(res => {
const posts = JSON.stringify(res.body);
console.log(posts);
})
.catch((err) => {
console.log(err);
throw err;
});
}
因此,不要在标头中设置对象,而是将其作为 2 个参数(名称、值)传递。
而不是设置完整的标题
.set({'Authorization': 'Bearer ' + this.state.id_token})
您可以使用
.auth(this.state.id_token, { type: 'bearer' })
尝试使用.auth('Bearer', this.state.id_token)
http://visionmedia.github.io/superagent/#authentication
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.