400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token

76 views Asked by At

I have a python program that was working fine. Today, I have noticed that something has changed. The idea of the project was that I could transfer the whole collection of my liked songs on Spotify to a playlist. Here is the code:

import spotipy
from spotipy.oauth2 import SpotifyOAuth
from app import app


def transfer_tracks(destination_playlist_uri):

    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id='id',
                                                    client_secret='secret',
                                                    redirect_uri='redirect_uri',
                                                    scope='user-library-read playlist-modify-public'))
    


   # Retrieve the total count of liked tracks
    
    saved_tracks = sp.current_user_saved_tracks(limit=1)
    total_tracks = saved_tracks['total']

    # Set the batch size for pagination
    batch_size = 20
    offset = 0

    while offset < total_tracks:
        results = sp.current_user_saved_tracks(limit=batch_size, offset=offset)
        track_uris = [item['track']['uri'] for item in results['items']]

        # Add the track URIs to the destination playlist
        destination_playlist_id = destination_playlist_uri.split('/')[-1]
        sp.playlist_add_items(destination_playlist_id, track_uris)

        offset += batch_size

    print("All tracks transferred to the destination playlist!")



# Set the URIs for the destination playlist
destination_playlist_uri = 'https://open.spotify.com/playlist/playlist-id'

# Call the transfer_tracks function
transfer_tracks(destination_playlist_uri)

# Run the Flask server
if __name__ == '__main__':
    app.run(port=8080)

This is a problem I encounter:

raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://accounts.spotify.com/api/token

During handling of the above exception, another exception occurred:

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

I also tried to create another application on Spotify Dashboard, but it didn't help. What should I do and what do I miss?

1

There are 1 answers

0
Timur On BEST ANSWER

I just deleted cache file in the same folder where my project was and it helped