根据OAuth 2.0规范,传递访问令牌(access token)时应该使用Authorization标头中的Bearer方案。因此,在客户端axios请求中添加authorization bearer {token} header是一个标准的做法。
示例代码如下:
import axios from 'axios';
const token = ''; // 获取的access token
axios({
method: 'post',
url: 'https://example.com/api',
data: {
email: 'example@example.com',
password: '123456'
},
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
})
.then(response => console.log(response))
.catch(error => console.error(error));