Python: Refresh access token using client credentials and spotipy for Spotify API

Viewed 22

How can I refresh the access token using client credentials and spotipy for Spotify API? I am creating a script which utilises the Spotipy library to make song recommendations from the Spotify API, but since I am using the client credentials flow. The access key therefore runs out every hour. I have followed the spotipy documentation to try to refresh the access token as it states that get_access_token() can check "if a valid access token is in memory, returns it Else fetches a new token"

import spotipy
import os
from spotipy.oauth2 import SpotifyClientCredentials
from os import path

def run_spotify():
    '''
    Access the Spotify API with a client ID and client secret
    from the client_details.json file
    '''
    creds = {'client_id': os.environ.get("CLIENT_ID"),
             'client_secret': os.environ.get("CLIENT_SECRET")}
    credentials = SpotifyClientCredentials(**creds)
    token = credentials.get_access_token()
    spotify = spotipy.Spotify(token['access_token'])
    return spotify

After the hour, I obtained the following error message:

spotipy.oauth2.SpotifyOauthError: error: invalid_client, error_description: Invalid client

Thanks a mil!

0 Answers
Related