【发布时间】:2020-07-15 20:09:25
【问题描述】:
正在开发一个将当前歌曲添加到某个播放列表的 spotify / spotipy 应用程序。 我获取当前歌曲和显示播放列表的功能都运行良好。我将歌曲添加到播放列表的功能不是。
我的 IDE 中出现以下错误:
http status: 405, code:-1 - https://api.spotify.com/v1/users/myUserID/playlists/myPlayListID/tracks:
error
当我将链接复制粘贴到浏览器时,出现以下错误:
{
"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"]])
此时我唯一能想到的是显示播放列表/显示当前正在播放的歌曲操作需要较少的权限,这就是它们起作用的原因,而将歌曲添加到播放列表则不起作用。
【问题讨论】:
标签: python http token spotify spotipy