How to fix 'error: invalid_grant Invalid authorization code' when asking for reshresh_token from Spotify API?
Asked Answered
E

1

7

I'm trying to receive refresh_token for my Ionic app, I successfully receive access_token.

I receive code (authorization_code or access_token) from endpoint https://accounts.spotify.com/authorize?client_id= in my TypeScript project, I pass it to cURL to test how to get refresh_token but the only thing which I receive its error.

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

Expected output:

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

But received error:

{"error":"invalid_grant","error_description":"Invalid authorization code"}
Euchromosome answered 23/8, 2019 at 0:7 Comment(0)
E
12

So after two days of trying I found this issue and after checking documentation I found where I was wrong. The thing is when you want to refresh token you need to send in body of POST request to /api/token endpoint code not access_token. In my case I was sending access_token. To receive code you should send same request to https://accounts.spotify.com/authorize endpoint but with parameter response_type=code.

Euchromosome answered 23/8, 2019 at 11:30 Comment(1)
If you answered your own question correctly, you should mark this as the answer.Legislator

© 2022 - 2024 — McMap. All rights reserved.