[Python] How do I fix a Spotipy Access Token Error?

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I've been having problems getting the access code with Spotipy on Streamlit. The Redirect URI are the same on the dashboard, and the code worked normally when I tried locally, but as I tried to deploy the app, the problems arose. This is an app that is supposed to show the user's top 10 most heard songs in the last 4 weeks.

    The error:


    Status: 400
    {"error":"invalid_grant","error_description":"Invalid authorization code"}
    ​

    The code:

    import spotipy
    import streamlit as st
    import requests
    import os
    import urllib.parse
    import base64

    # --- CONFIGS ---
    CLIENT_ID = os.environ.get("SPOTIPY_CLIENT_ID", "")
    CLIENT_SECRET = os.environ.get("SPOTIPY_CLIENT_SECRET", "")
    REDIRECT_URI = "https://legato-top10tracks.streamlit.app/callback"

    # --- AUTH URL ---
    params = {
    "client_id": CLIENT_ID,
    "response_type": "code",
    "redirect_uri": REDIRECT_URI,
    "scope": "user-top-read user-library-read user-read-recently-played"
    }
    auth_url = f"https://accounts.spotify.com/authorize?{urllib.parse.urlencode(params)}"

    # --- LAYOUT ---
    st.set_page_config(page_title="Legato - Spotify", layout="wide")
    st.title(" Legato - Suas 10 músicas mais tocadas")
    query_params = st.query_params

    # --- FLUXO ---
    if "code" not in query_params:
    st.markdown(f"[ Clique aqui para autenticar com o Spotify]({auth_url})")
    st.stop()

    # --- CHANGES CODE FOR TOKEN ---
    code = query_params["code"][0]

    # Preparar cabeçalho de autenticação
    auth_str = f"{CLIENT_ID}:{CLIENT_SECRET}"
    b64_auth_str = base64.b64encode(auth_str.encode()).decode()

    headers = {
    "Authorization": f"Basic {b64_auth_str}",
    "Content-Type": "application/x-www-form-urlencoded"
    }
    data = {
    "grant_type": "authorization_code",
    "code": code,
    "redirect_uri": REDIRECT_URI
    }

    token_response = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=data)

    if token_response.status_code != 200:
    st.error("❌ Erro ao obter o token de acesso do Spotify.")
    st.write("Status:", token_response.status_code)
    st.write("Resposta:", token_response.json())
    st.stop()

    # Clear URL after sucess
    st.query_params

    # Use token
    access_token = token_response.json()["access_token"]
    sp = spotipy.Spotify(auth=access_token)

    # --- GATHERS TOP TRACKS ---
    top_tracks = sp.current_user_top_tracks(limit=10, time_range="short_term")

    st.subheader(" Suas Top 10 Músicas")

    for i, item in enumerate(top_tracks['items'], 1):
    track_name = item['name']
    album_name = item['album']['name']
    artists = ', '.join([artist['name'] for artist in item['artists']])
    release_date = item['album']['release_date']
    duration_ms = item['duration_ms']
    popularity = item['popularity']
    image_url = item['album']['images'][0]['url']
    spotify_url = item['external_urls']['spotify']

    st.markdown(f"### {i}º Lugar")
    col1, col2 = st.columns([1, 3])
    with col1:
    st.image(image_url, width=250)
    with col2:
    st.markdown(f"**Música:** [{track_name}]({spotify_url})")
    st.markdown(f"**Álbum:** {album_name}")
    st.markdown(f"**Artistas:** {artists}")
    st.markdown(f"**Lançamento:** {release_date}")
    st.markdown(f"**Duração (ms):** {duration_ms}")
    st.markdown(f"**Popularidade:** {popularity}")
    st.markdown("---")


    I've been asking ChatGPT for help and it doesn't get me anywhere. It should be working by now (according to ChatGPT), and also have a suspicion that this error is maybe related to the recent updates of the Spotify API.

    Continue reading...

Compartilhe esta Página