Troubleshooting Spotify API: 'invalid_grant' error when requesting refresh token

I’m stuck with a problem in my Ionic app. I’m trying to get a refresh_token from the Spotify API. I can get the access_token just fine, but when I try to exchange the authorization_code for a refresh_token, I keep getting an error.

Here’s what I’m doing:

  1. I get the code from https://accounts.spotify.com/authorize
  2. I use this code in a POST request to https://accounts.spotify.com/api/token

My request looks something like this:

POST /api/token HTTP/1.1
Host: accounts.spotify.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic Base64EncodedClientIDAndSecret

grant_type=authorization_code&code=MyAuthorizationCode&redirect_uri=http://localhost:8100/main

But instead of getting the refresh_token, I’m getting this error:

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

What am I doing wrong? How can I fix this and get the refresh_token?

hey, check that the code isnt expired. its only valid for about 10 minutes. get a fresh one and make sure the redirect_uri is exactly what’s set in ur dashbord.