刷新令牌 Spotify
Refresh token spotipy
我正在使用 spotipy 从 Spotify 中使用 python[=20= 检索一些曲目].因此,我收到令牌过期错误,我想 刷新 我的令牌。但是我不明白如何从 spotipy 获取刷新令牌。
是否有其他方法来刷新令牌或重新创建令牌?
谢谢。
Spotipy 使用访问令牌的粗略过程是:
- 从缓存中获取token(其实不仅仅是access token,还有刷新和过期信息)
- 如果令牌在缓存中并已过期,请刷新它
- 如果令牌不在缓存中,那么
prompt_for_user_token()
将处理您在浏览器中完成 OAuth 流程,然后将其保存到缓存中。
因此,如果您向 Spotipy 询问访问令牌(例如使用 prompt_for_user_token()
或直接设置 SpotifyOAuth
对象)并且它已缓存访问令牌/刷新令牌,它将自动刷新之前。默认情况下,缓存位置应为工作目录中的 .cache-<username>
,因此您可以在那里手动访问令牌。
如果您向 Spotipy Spotify()
客户端提供 auth
参数进行授权,它将无法自动刷新访问令牌,我认为它会在大约一个小时后过期。您可以改为提供 client_credentials_manager
,它将从中请求访问令牌。 client_credentials_manager
对象实现的唯一要求是它提供了一个 get_access_token()
方法,该方法不带任何参数和 returns 一个访问令牌。
我前一段时间在 fork 中尝试过,here's the modification to the SpotifyOAuth
object to allow it to act as a client_credentials_manager
and here's the equivalent of prompt_for_user_token()
returns SpotifyOAuth
对象,您可以将其作为凭据管理器传递给 Spotipy Spotify()
客户端参数
因为这个问题花了我一段时间才弄清楚,所以我将把我的解决方案放在这里。这对服务器上的 运行ning Spotipy 永远有效(或者至少在过去 12 小时内有效)。您必须 运行 在本地生成 .cache 文件一次,但是一旦发生,您的服务器就可以使用该缓存文件来更新它的访问令牌并在需要时刷新令牌。
import spotipy
scopes = 'ugc-image-upload user-read-playback-state user-modify-playback-state user-read-currently-playing ...'
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
while True:
try:
current_song = sp.currently_playing()
do something...
except spotipy.SpotifyOauthError as e:
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
我看到了 mardiff 的解决方案,它绝对有效,但我不喜欢它等待错误发生然后修复它,所以我找到了一个不需要捕获错误的解决方案,使用的方法是 spotipy已经实施了。
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
USERNAME = '...'
CLIENT_ID = '...'
CLIENT_SECRET = '...'
SCOPE = 'user-read-currently-playing'
def create_spotify():
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USERNAME,
redirect_uri='http://localhost:8080',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
return auth_manager, spotify
def refresh_spotify(auth_manager, spotify):
token_info = auth_manager.cache_handler.get_cached_token()
if auth_manager.is_token_expired(token_info):
auth_manager, spotify = create_spotify()
return auth_manager, spotify
if __name__ == '__main__':
auth_manager, spotify = create_spotify()
while True:
auth_manager, spotify = refresh_spotify(auth_manager, spotify)
playing = spotify.currently_playing()
if playing:
print(playing['item']['name'])
else:
print('Nothing is playing.')
time.sleep(30)
使用此方法,您可以在每次使用 spotify 对象之前检查令牌是否过期(或在过期后 60 秒内)。根据需要创建新的 auth_manager 和 spotify 对象。
我正在使用 spotipy 从 Spotify 中使用 python[=20= 检索一些曲目].因此,我收到令牌过期错误,我想 刷新 我的令牌。但是我不明白如何从 spotipy 获取刷新令牌。
是否有其他方法来刷新令牌或重新创建令牌?
谢谢。
Spotipy 使用访问令牌的粗略过程是:
- 从缓存中获取token(其实不仅仅是access token,还有刷新和过期信息)
- 如果令牌在缓存中并已过期,请刷新它
- 如果令牌不在缓存中,那么
prompt_for_user_token()
将处理您在浏览器中完成 OAuth 流程,然后将其保存到缓存中。
因此,如果您向 Spotipy 询问访问令牌(例如使用 prompt_for_user_token()
或直接设置 SpotifyOAuth
对象)并且它已缓存访问令牌/刷新令牌,它将自动刷新之前。默认情况下,缓存位置应为工作目录中的 .cache-<username>
,因此您可以在那里手动访问令牌。
如果您向 Spotipy Spotify()
客户端提供 auth
参数进行授权,它将无法自动刷新访问令牌,我认为它会在大约一个小时后过期。您可以改为提供 client_credentials_manager
,它将从中请求访问令牌。 client_credentials_manager
对象实现的唯一要求是它提供了一个 get_access_token()
方法,该方法不带任何参数和 returns 一个访问令牌。
我前一段时间在 fork 中尝试过,here's the modification to the SpotifyOAuth
object to allow it to act as a client_credentials_manager
and here's the equivalent of prompt_for_user_token()
returns SpotifyOAuth
对象,您可以将其作为凭据管理器传递给 Spotipy Spotify()
客户端参数
因为这个问题花了我一段时间才弄清楚,所以我将把我的解决方案放在这里。这对服务器上的 运行ning Spotipy 永远有效(或者至少在过去 12 小时内有效)。您必须 运行 在本地生成 .cache 文件一次,但是一旦发生,您的服务器就可以使用该缓存文件来更新它的访问令牌并在需要时刷新令牌。
import spotipy
scopes = 'ugc-image-upload user-read-playback-state user-modify-playback-state user-read-currently-playing ...'
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
while True:
try:
current_song = sp.currently_playing()
do something...
except spotipy.SpotifyOauthError as e:
sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(scope=scopes))
我看到了 mardiff 的解决方案,它绝对有效,但我不喜欢它等待错误发生然后修复它,所以我找到了一个不需要捕获错误的解决方案,使用的方法是 spotipy已经实施了。
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
USERNAME = '...'
CLIENT_ID = '...'
CLIENT_SECRET = '...'
SCOPE = 'user-read-currently-playing'
def create_spotify():
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USERNAME,
redirect_uri='http://localhost:8080',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
return auth_manager, spotify
def refresh_spotify(auth_manager, spotify):
token_info = auth_manager.cache_handler.get_cached_token()
if auth_manager.is_token_expired(token_info):
auth_manager, spotify = create_spotify()
return auth_manager, spotify
if __name__ == '__main__':
auth_manager, spotify = create_spotify()
while True:
auth_manager, spotify = refresh_spotify(auth_manager, spotify)
playing = spotify.currently_playing()
if playing:
print(playing['item']['name'])
else:
print('Nothing is playing.')
time.sleep(30)
使用此方法,您可以在每次使用 spotify 对象之前检查令牌是否过期(或在过期后 60 秒内)。根据需要创建新的 auth_manager 和 spotify 对象。