【问题标题】:Spotipy 401 / 405 No token providedSpotipy 401 / 405 未提供令牌
【发布时间】: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


    【解决方案1】:

    发现我错误地实现了用户。而不是 username?si=someNumbers 我只需要输入用户名。

    编辑:

    • 将错误消息的地址复制并粘贴到浏览器中是没有用的,因为它是一个 api 调用,它总是会返回 401: no token provided 由于试图在没有令牌的情况下访问它。
    • 刚刚偶然发现了这个包含每个错误代码含义的精美列表。 https://developer.spotify.com/documentation/web-api/ 这不仅适用于 web api,也适用于 Sptipy 库。根据此页面上的列表检查 IDE 或编辑器中的错误代码。

    【讨论】:

      猜你喜欢
      • 2019-06-23
      • 2018-03-06
      • 2018-01-31
      • 1970-01-01
      • 2017-12-28
      • 2018-07-17
      • 2020-03-17
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多