0

我正在尝试使用 Spotipy 方法删除重复出现的曲目(因此删除重复项)。但是该功能似乎不起作用;Spotify API 调用返回没有授权令牌的错误。
Spotify API 返回错误:

{
  "error": {
    "status": 401,
    "message": "No token provided"
  }
}

Python的错误:

  File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\Spotify.py", line 87, in remove_all_duplicate_tracks
    sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, tracks)
  File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 539, in user_playlist_remove_specific_occurrences_of_tracks
    payload=payload)
  File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 183, in _delete
    return self._internal_call('DELETE', url, payload, kwargs)
  File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 124, in _internal_call
    headers=r.headers)
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/___________________________/tracks:
 Could not remove tracks, please check parameters.

这是我的代码:

def remove_all_duplicate_tracks(playlist_id, token):
    sp = spotipy.Spotify(token)
    username = get_username(token)
    existing_tracks = get_track_uris_for_playlist(playlist_id, token)
    duplicate_counter = Counter(existing_tracks)
    tracks = []
    for uri, count in duplicate_counter.items():
        count = count-1
        if count > 0:
            # hard coded position as 1 for testing...
            positions = [1]
            #positions = [x for x in range(1, count+1)]
            track_dict = {"uri": uri, "positions": positions}
            tracks.append(track_dict)
    sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, tracks)

这就是“tracks”所包含的内容:
[{'uri': '6jq6rcOikCZAmjliAgAmfT', 'positions': [1]}, {'uri': '3tSmXSxaAnU1EPGKa6NytH', 'positions': [1]}, {'uri': '7jeI6EdY0elPSNz80mAKS8', 'positions': [1]}]
我测试了其他方法get_username()get_track_uris_for_playlist它们返回了您所期望和正在工作的内容。

4

2 回答 2

1

虽然这个答案来得很晚,但它是必要的,因为 1)问题没有解决,2)我相信它会对有类似问题的人有所帮助。

首先,您应该将您的问题限制在具体问题上,即调用该sp.user_playlist_remove_specific_occurrences_of_tracks()函数产生的授权错误。这会让问题更清楚。(按照它的说法,必须挖掘代码才能找到“热点”!而且有关曲目的细节只会增加混乱。)

所以,我将把我的答案限制在这个问题上,并建议使用以下代码作为基础:

# Data
username = (your username)
playlist_id = (playlist id)  # The ID of the playlist containing the tracks to be deleted
track_ids = [(track_id), (track_id), ...]  # List of track IDs to delete

# Authorization process
scope = "playlist-read-private"
token = spotipy.util.prompt_for_user_token(username, scope=scope)
sp = spotipy.Spotify(auth=token)

# Call the track deletion function
sp.user_playlist_remove_all_occurrences_of_tracks(username, playlist_id, track_ids)

我自己正在使用这个过程。我刚刚用我的数据尝试了上面的代码,它也应该适合你。

于 2020-07-09T08:05:47.563 回答
-1

您正在尝试更改用户数据:

Could not remove tracks, please check parameters.

传递一个有效的范围,例如:

播放列表修改公共

播放列表修改私有

有关范围的更多信息:https ://developer.spotify.com/documentation/general/guides/scopes/

import spotipy
import spotipy.util as util

scope = 'playlist-modify-public playlist-modify-private'

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.current_user_saved_tracks()
    for item in results['items']:
        track = item['track']
        print track['name'] + ' - ' + track['artists'][0]['name']
else:
    print "Can't get token for", username
于 2018-07-22T00:32:57.387 回答