Joe Liang
Joe Liang

Reputation: 115

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:

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...

Upvotes: 2

Views: 868

Answers (1)

Rohith Nambiar
Rohith Nambiar

Reputation: 3730

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
}

Upvotes: 1

Related Questions