Spotify API授权以创建播放列表

时间:2020-03-31 19:36:06

标签: python api authorization spotify spotipy

我一直在尝试使用Spotify for Developers Tools编写代码,该代码将读取用户选择的播放列表中所有歌曲的音频功能,然后创建两个新的播放列表以根据原始播放列表中的歌曲进行排序他们被认为是次要的或主要的。我已经完成了大部分程序的工作,它会读取播放列表并创建歌曲音频功能的字典,并为次要歌曲创建两个列表,为主要歌曲创建一个列表,但是由于该原因,我不会创建一个新的播放列表的授权,我不知道如何解决这个问题。以下是我的代码,非常感谢您的帮助!

以下是我的帐户信息的开头:

################### Account Information ######################

cid ='*my client id*' # Client ID
secret = '*my secret id*' # Client Secret
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
username = '*my username*' 
token = util.prompt_for_user_token(username=username, scope='playlist-modify-public', client_id=cid, client_secret=secret, redirect_uri="http://localhost:8888/callback")

#############################################################

然后我尝试创建播放列表:

############### Create Playlist #################

created_playlist_minor = sp.user_playlist_create(username, "New Playlist Minor", description='Minor')
created_playlist_major = sp.user_playlist_create(username, "New Playlist Major", description='Major')
created_playlist_other = sp.user_playlist_create(username, "New Playlist Other", description='Other')

#################################################

然后它给我一个错误

spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/users/*username*/playlists:
This request requires user authentication.

1 个答案:

答案 0 :(得分:0)

您正在将availableDates: {{ availableDates }} 对象传递给SpotifyClientCredentials客户端。这将对Spotify进行身份验证,以便您可以执行开发人员API密钥可以访问的任何操作(例如,在公共播放列表中获取有关歌曲的信息)。您没有为您的spotipy.Spotify提供用户的身份验证令牌,因此您无法执行用户特定的操作,例如创建播放列表。您应该执行以下操作:

spotipy.Spotify

请注意,您不必将客户ID和密码传递给username = '*my username*' token = util.prompt_for_user_token( username=username, scope='playlist-modify-public', client_id=cid, client_secret=secret, redirect_uri="http://localhost:8888/callback" ) sp = spotipy.Spotify(auth=token) ,令牌就足够了