首页 > 解决方案 > 使用 spotipy 和 Spotify Web API 时令牌不会刷新

问题描述

我正在尝试使用Spotipy 文档中的代码来查看客户端授权代码流是如何工作的。起初它有效,但过了一段时间我收到此错误:无法刷新令牌。响应状态代码:400 原因:错误请求。除了将客户端凭据添加到代码中之外,我根本没有编辑文档中的代码。谢谢!这是我的代码:

import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "user-library-read"

client_id='xxxxxxx'

client_secret='xxxxxx'

redirect_uri = "http://localhost:5000/callback"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope,client_id=client_id,client_secret=client_secret,redirect_uri=redirect_uri))

results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):

    track = item['track']

    print(idx, track['artists'][0]['name'], " – ", track['name'])

标签: pythonspotifyspotipy

解决方案


您需要刷新访问代码。

您可以在此处的 spotipy 库中执行此操作 https://spotipy.readthedocs.io/en/2.16.1/?highlight=refresh#spotipy.oauth2.SpotifyOAuth.refresh_access_token

这就是为什么您通常会收到 400 原因:错误请求 | 403原因:禁止


推荐阅读