需要帮助。 我如何在React中将Athentication承载与superagent一起使用。 我不确定是否在sintax中,也找不到示例。
我现在做什么
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;
});
}
Thnx!
答案 0 :(得分:1)
设置标题的方式是通过提供标题项目的名称和值,尝试:
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个参数(名称,值)传递。
答案 1 :(得分:0)
尝试使用.auth('Bearer', this.state.id_token)
http://visionmedia.github.io/superagent/#authentication
答案 2 :(得分:0)
而不是设置完整的标头
.set({'Authorization': 'Bearer ' + this.state.id_token})
您可以使用
.auth(this.state.id_token, { type: 'bearer' })