我正在尝试使用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)
“曲目”包含以下内容:
[{'uri': '6jq6rcOikCZAmjliAgAmfT', 'positions': [1]}, {'uri': '3tSmXSxaAnU1EPGKa6NytH', 'positions': [1]}, {'uri': '7jeI6EdY0elPSNz80mAKS8', 'positions': [1]}]
我测试了其他方法get_username()
和get_track_uris_for_playlist
,它们返回了您期望的值并且正在运行。
答案 0 :(得分: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)
我自己正在使用此过程。我已经使用我的数据尝试了上面的代码,它也对您有用。
答案 1 :(得分:0)
您正在尝试更改用户数据:
Could not remove tracks, please check parameters.
传递有效范围,例如:
playlist-modify-public
playlist-modify-private
有关范围的更多信息: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