当从Spotify API请求reshresh_Token时,如何修复错误:INVALID_GRANT INVALID授权码'?

我正在尝试接收refresh_token对于我的Ionic应用程序,我成功接收access_token

我从终结点https://accounts.spotify.com/authorize?client_id=接收code(authorization_codeaccess_token)在我的TypeScrip项目中,我将其传递给cURL以测试如何获取refresh_token,但我唯一收到其错误的内容。

curl -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Basic MzBhNT...ZWIyZWQ=" -d grant_type=authorization_code -d code=BQAjgZOFne3p8PB4ARJkrXtq...kI4xJvhQ7SutZoJqVWILCY -d redirect_uri=http%3A%2F%2Flocalhost%3A8100%2Fmain -X POST https://accounts.spotify.com/api/token

预期输出:

{
   "access_token": "NgCXRK...MzYjw",
   "token_type": "Bearer",
   "scope": ...,
   "expires_in": 3600,
   "refresh_token": "NgAagA...Um_SHo"
}

但收到错误:

{"error":"invalid_grant","error_description":"Invalid authorization code"}

解决方案

因此,经过两天的尝试,我发现了issue,并且在查看文档后,我找到了我错的地方。问题是,当您想要刷新令牌时,您需要在POST请求正文中发送到/api/Token端点代码,而不是Access_Token。在我的例子中,我发送的是Access_Token。若要接收代码,您应该向https://accounts.spotify.com/authorize终结点发送相同的请求,但参数为response_type=code

相关文章