pythonauthenticationspotify

Trying to get Access token and Refresh token from Spotify API (Python)


I managed to get the auth code from the URI and I'm using it has explained from the documentation. Double checked client_id and client_secret to be sure everything is allright. Same thing for URI's url and Token requests' url. I can't wrap my head around on why this happens

Here's my code

parameters = {
"grant_type":"authorization_code",
"code": code,
"redirect_uri":SPOTIFY_URI
}

auth_string = base64.b64encode(f"{SPOTIFY_ID}:{SPOTIFY_SECRET}".encode("utf-8"))

headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": f"Basic {auth_string.decode('utf-8')}",
}

response = requests.post(
    "https://accounts.spotify.com/api/token", params=parameters,
    headers=headers
)

response.raise_for_status()
print(response)

And here's the error message that is raised:

requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:<my_token_url>

Can anyone help?


Solution

  • I resolved the issue. I re-requested to be redirected to the redirect_uri following the previous step ending up with a new code. I then run the code again and received <Response [200]> getting hold of the access and refresh tokens

    I didn't seem to realize that the "code" was a one-time use and needed to be requested again at every iteration of the code