我试图从spotify API中获取token,我使用的是axios。我使用API给出的例子作为指导,但给我的错误404。
export const getToken = code => async dispatch => {
const responseToken = await axios.post({
url: "https://accounts.spotify.com/api/token",
form: {
grant_type: "authorization_code",
code,
redirect_uri
},
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
json: true
})
console.log(responseToken);
第一行是因为我用的是redux,我只是想让你看到这是一个asinc方法。
我一整天都在尝试解决这个问题,我没有更多的想法来解决这个问题。
试着改变
form: {
grant_type: "authorization_code",
code,
redirect_uri
}
到
data: JSON.stringify({
grant_type: "authorization_code",
code,
redirect_uri
})
你想在请求体中发送它,因此是 "数据",这就是你在axios中的定义.另外,我认为你不需要 json: true
EDIT: 很确定你必须添加 'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
头部也是如此。