您可以使用 Spotify 的 python 库之一,例如:https://github.com/plamere/spotipy https://github.com/plamere/spotipy
在存储库中,您可以看到用户身份验证的示例
import spotipy
from spotipy.oauth2 import SpotifyOAuth
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID",
client_secret="YOUR_APP_CLIENT_SECRET",
redirect_uri="YOUR_APP_REDIRECT_URI",
scope="user-library-read"))
results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
track = item['track']
print(idx, track['artists'][0]['name'], " – ", track['name'])
(需要先安装库:pip install spotipy
)
另一个Python库https://tekore.readthedocs.io/en/stable/ https://tekore.readthedocs.io/en/stable/:
import tekore as tk
conf = (client_id, client_secret, redirect_uri)
token = tk.prompt_for_user_token(*conf, scope=tk.scope.every)
spotify = tk.Spotify(token)
tracks = spotify.current_user_top_tracks(limit=10)
spotify.playback_start_tracks([t.id for t in tracks.items])