Spotipy 401 / 405 未提供令牌
Spotipy 401 / 405 No token provided
一直在开发 spotify / spotipy 应用程序,它将当前歌曲添加到某个播放列表。
我获取当前歌曲和显示播放列表的功能都很好。我添加歌曲到播放列表的功能没有。
我的 IDE 中出现以下错误:
http status: 405, code:-1 - https://api.spotify.com/v1/users/myUserID/playlists/myPlayListID/tracks:
error
当我将 link 复制粘贴到浏览器时,出现以下错误:
{
"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"]])
在这一点上,我唯一能想到的是显示播放列表/显示当前播放的歌曲操作需要较少的权限,这就是它们工作的原因,而将歌曲添加到播放列表不会。
发现我实施的用户错误。而不是用户名?si=someNumbers 我只需要输入用户名。
编辑:
- 将错误消息的地址复制并粘贴到浏览器中是无用的,因为它是一个 api 调用,它总是会返回 401:由于尝试在没有令牌的情况下访问它而未提供令牌.
- 刚刚偶然发现了这个包含每个错误代码含义的精美列表。 https://developer.spotify.com/documentation/web-api/ 这不仅适用于网络 api,也适用于 Spotipy 库。根据此页面上的列表检查 IDE 或编辑器中的错误代码。
一直在开发 spotify / spotipy 应用程序,它将当前歌曲添加到某个播放列表。 我获取当前歌曲和显示播放列表的功能都很好。我添加歌曲到播放列表的功能没有。
我的 IDE 中出现以下错误:
http status: 405, code:-1 - https://api.spotify.com/v1/users/myUserID/playlists/myPlayListID/tracks:
error
当我将 link 复制粘贴到浏览器时,出现以下错误:
{
"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"]])
在这一点上,我唯一能想到的是显示播放列表/显示当前播放的歌曲操作需要较少的权限,这就是它们工作的原因,而将歌曲添加到播放列表不会。
发现我实施的用户错误。而不是用户名?si=someNumbers 我只需要输入用户名。
编辑:
- 将错误消息的地址复制并粘贴到浏览器中是无用的,因为它是一个 api 调用,它总是会返回 401:由于尝试在没有令牌的情况下访问它而未提供令牌.
- 刚刚偶然发现了这个包含每个错误代码含义的精美列表。 https://developer.spotify.com/documentation/web-api/ 这不仅适用于网络 api,也适用于 Spotipy 库。根据此页面上的列表检查 IDE 或编辑器中的错误代码。