我一直在开发一个 Spotify / spotipy 应用程序,它将当前歌曲添加到某个播放列表。 我获取当前歌曲和显示播放列表的功能都很好。我的将歌曲添加到播放列表的功能不是。
我的 IDE 中出现以下错误:
http 状态:405,代码:-1 - https://api.spotify.com/v1/users/myUserID/playlists/myPlayListID/tracks:
错误
当我复制粘贴链接到浏览器时,出现以下错误:
{
"error": {
"status": 401,
"message": "No token provided"
}
}
据我所知,我正在提供令牌。
这是我制作 Spotify 对象的函数
def __createSpotifyObject(self):
"""Will create a Spotify object and return it"""
# Defining the scope(s) of the application
scope = "playlist-modify-public playlist-modify-private user-read-currently-playing"
# Getting the token
token = util.prompt_for_user_token(username=USER_NAME, scope=scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri="https://localhost/")
# Returning our Spotify object
return spotipy.Spotify(auth=token)
这是尝试将歌曲添加到我的播放列表的功能(这是发生错误的地方)
我已经尝试捕获异常(确实如此),然后尝试创建一个新的 Spotify 对象,以便它刷新令牌或类似的东西。它只是给了我同样的错误。
def addCurrentSongToSelectedPlaylist(self):
"""Will add the currently playing song to the selected playlist"""
# Checking if a playlist has been selected
if (len(self.__selectedPlaylist ) < 1):
print("No playlist selected")
return
# Getting the current song
currentSong = self.getCurrentSong()
# Adding the current song id to the selected playlist
try:
self.__spotify.user_playlist_add_tracks(USER, self.__selectedPlaylist, [currentSong["id"]])
except spotipy.client.SpotifyException:
# Re authenticating
self.__spotify = self.__createSpotifyObject()
self.__spotify.user_playlist_add_tracks(USER, self.__selectedPlaylist, [currentSong["id"]])
在这一点上,我唯一能想到的是显示播放列表/显示当前播放的歌曲操作需要较少的权限,这就是它们工作的原因,而将歌曲添加到播放列表不会。
我发现我实现了用户错误。而不是username?si=someNumbers,我只需要输入用户名。