Plan
Premium
Country: USA
Device
(Macbook Pro 1st Gen M1)
Operating System
(MacOS Ventura 13.0.1)
My Question or Issue
I am trying to write a python script that just scrapes other users' playlists and adds songs in them to my own playlist. When running the following script:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# Set your Spotify API credentials
client_id = "my_client_id"
client_secret = "my_client_secret"
scopes = ["user-playlist-create", "user-playlist-modify"]
# Authenticate with the Spotify API
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Define the name and description of your playlist
playlist_name = "new_playlist"
playlist_description = "a new playlist built from other users' playlists"
# Create the playlist on your Spotify account
playlist = sp.user_playlist_create(user="spiritkhakis", name=playlist_name, description=playlist_description)
playlist_id = playlist["id"]
# Add songs from other users' playlists to your playlist
songs = []
for user in ["user1", "user2", "user3"]:
playlists = sp.user_playlists(user=user)
for playlist in playlists["items"]:
if playlist["name"] == "Your Top Songs 2022":
playlist_tracks = sp.playlist_tracks(playlist_id=playlist["id"])
for track in playlist_tracks["items"]:
songs.append(track["track"]["id"])
# Add the songs to your playlist
sp.user_playlist_add_tracks(user="spiritkhakis", playlist_id=playlist_id, tracks=songs)
# Print a message to confirm that the playlist has been created
print(f"Successfully created playlist: {playlist_name}")
# Print a message to confirm that the playlist has been created
print(f"Successfully created playlist: {playlist_name}")
I receive the following error message:
Couldn't read cache at: .cache
Couldn't write token to cache at: .cache
HTTP Error for POST to https://api.spotify.com/v1/users/spiritkhakis/playlists with Params: {} returned 403 due to This request requires user authentication.
Traceback (most recent call last):
File "/Users/grayson/master/lib/python3.9/site-packages/spotipy/client.py", line 245, in _internal_call
response.raise_for_status()
File "/Users/grayson/master/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/users/spiritkhakis/playlists
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/grayson/master/playlist.py", line 18, in <module>
playlist = sp.user_playlist_create(user="spiritkhakis", name=playlist_name, description=playlist_description)
File "/Users/grayson/master/lib/python3.9/site-packages/spotipy/client.py", line 792, in user_playlist_create
return self._post("users/%s/playlists" % (user,), payload=data)
File "/Users/grayson/master/lib/python3.9/site-packages/spotipy/client.py", line 302, in _post
return self._internal_call("POST", url, payload, kwargs)
File "/Users/grayson/master/lib/python3.9/site-packages/spotipy/client.py", line 267, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/spiritkhakis/playlists:
This request requires user authentication., reason: None
Not sure what is causing this, since I'm pretty sure I have authenticated the user in question (spiritkhakis is my own account, and the account with which I have registered the app). I saw one forum post saying I needed to navigate specifically to an "Authenticate a New User" option under the "Dashboard" tab on the Spotify Developer website, but when I navigate to my dashboard, there is no such option.
All help is appreciated. Thanks!