我目前正在制作(试图制作)一个react应用程序,其功能是获取用户的热门曲目,但是当我发送请求时,它会返回
Request failed with status code 403
AxiosError: Request failed with status code 403
我的代码是
const [tops, setTops] = useState([])
async function topTracks(timerange, nsongs){
const {data} = await axios.get(`https://api.spotify.com/v1/me/top/tracks?time_range=${timerange}&limit=${nsongs}`, {
headers: {
Authorization: `Bearer ${token}`
}
})
console.log({data})
setTops(data)
}
//react part
{token ?
<button onClick={() => {topTracks("short_term", 30)}}> get top songs</button>
: <h2> please login </h2>
}
查看其他帖子,我的授权流程可能不正确,但我不确定应该更改什么:
const CLIENT_ID = "++++++++++++++++++++++++++"
const REDIRECT_URI = "http://localhost:3000"
const AUTH_ENDPOINT = "https://accounts.spotify.com/authorize"
const RESPONSE_TYPE = "token"
如有任何帮助,我们将不胜感激!
答案:我忘记传递正确的搜索范围 - 我需要“user-top-read”来进行我想要的查询。