【发布时间】:2017-12-01 02:48:32
【问题描述】:
// If you want your component to get data to render
componentWillMount() {
// Called first time the comp is loaded right before the comp is added to the page
console.log('Component WILL MOUNT!')
const BASE_URL = 'https://api.spotify.com/v1/search?';
const FETCH_URL = 'https://api.spotify.com/v1/artists/0OdUWJ0sBjDrqHygGUXeCF';
var accessToken = .......
axios.get(FETCH_URL, {headers : {'Authorization' : accessToken}})
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
})
}
在我的 React webapp 中,我从我提供的 accessToken 声明的 URL 请求 api。在我指定我的 accessToken 之前,它给了我 401(未经授权的错误),但现在它给了我 400(错误请求)。
我做错了什么?
【问题讨论】:
-
这是 OAuth2 吗?标题可能不正确。它应该是
Authorization: Bearer [token string]或类似的。 -
你的意思是这样的?
axios.get(FETCH_URL, {headers : {'Authorization' : Bearer [accessToken]}}) -
是的,根据 OAuth2 方法,值(冒号后)应该以单词 Bearer 开头,后跟空格,后跟令牌