Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spotify API authorization code flow error: "grant_type parameter is missing" (Python)

I’m following the docs to connect to Spotify using the authorization code flow. I can get the authorization code but not using it to get access token. The error I keep getting is

{'error': 'unsupported_grant_type', 'error_description': 'grant_type parameter is missing'}

Here’s my code in the callback function:

from requests import post

client_creds = f'{CLIENT_ID}:{CLIENT_SECRET}'
client_creds_64 = base64.b64encode(client_creds.encode())
token_data = {
     'grant-type': 'authorization_code',
     'code': code,
     'redirect_uri': REDIRECT_URI #currently on localhost and whitelisted on Spotify
}
token_header = {
     'Authorization': f'Basic {client_creds_64.decode()}',
     'Content-Type': 'application/x-www-form-urlencoded'
}

response = post('https://accounts.spotify.com/api/token', data=token_data, headers=token_header)

I can make the post request work if I use the client credentials flow and replace token_data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

token_data = {
     'grant_type': 'client_credentials'
}

Any idea why the authorization code flow doesn’t work? Don’t think I’m missing a parameter in token_data…

>Solution :

In grant_type there is a type error:

token_data = {
     'grant_type': 'authorization_code',
     'code': code,
     'redirect_uri': REDIRECT_URI #currently on localhost and whitelisted on Spotify
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading