Spotipy使用授权码流程刷新令牌 [英] Spotipy Refreshing a token with authorization code flow

查看:207
本文介绍了Spotipy使用授权码流程刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spotipy的长期运行的脚本.一个小时后(根据Spotify API),我的访问令牌失效.我已经成功地捕获了该令牌,但是在实际刷新令牌方面我不知道从那里可以走.我使用的是授权码流程,而不是客户凭证.这是我的授权方式:

I have a long-running script using spotipy. After an hour (per the Spotify API), my access token expires. I am catching this successfully, but I don't know where to go from there in regards to actually refreshing the token. I am using the authorization code flow, not client credentials. Here's how I authorize:

token = util.prompt_for_user_token(username,scope=scopes,client_id=client_id,client_secret=client_secret, redirect_uri=redirect_uri)

sp = spotipy.Spotify(auth=token)

我见过的所有刷新示例都涉及一个oauth2对象(例如oauth.refresh_access_token()),并且docs仅列出了该功能作为刷新令牌的方法.据我了解,有了授权代码流,您就不需要oauth对象(因为您要通过prompt_for_user_token()进行身份验证).如果是这种情况,如何刷新令牌?

All refresh examples I've seen involve an oauth2 object (ex. oauth.refresh_access_token()), and the docs list only that function as a method of refreshing your token. It's my understanding that with authorization code flow, you don't need an oauth object (because you authenticate with prompt_for_user_token()). If that's the case, how do I refresh my token?

推荐答案

我没有收到任何回复之后github问题,在我看来,如果不使用OAuth2,就无法刷新令牌.这违反了 Spotipy文档:

After receiving no response on my github issue, it appears to me that there's no way to refresh a token without using OAuth2. This goes against what is stated in the Spotipy docs:

授权码流程:此方法适用于用户一次登录的长时间运行的应用程序. 它提供了可以刷新的访问令牌.

他们的授权代码"流程示例使用hint_for_user_token().

Their example for Authorization Code flow uses prompt_for_user_token().

我切换到OAuth方法,这很痛苦,因为每次运行程序时都需要重新授权(这确实是我在测试时遇到的问题,但仍然是问题).由于Spotipy文档中没有OAuth2的示例,因此我将在此处粘贴我的示例.

I switched to the OAuth approach, which is a pain because it requires re-authorization every time I run the program (really only a problem while I was testing but a problem nonetheless). Since there are no examples of OAuth2 in the Spotipy docs, I'll paste mine here.

sp_oauth = oauth2.SpotifyOAuth(client_id=client_id,client_secret=client_secret,redirect_uri=redirect_uri,scope=scopes)
token_info = sp_oauth.get_cached_token() 
if not token_info:
    auth_url = sp_oauth.get_authorize_url(show_dialog=True)
    print(auth_url)
    response = input('Paste the above link into your browser, then paste the redirect url here: ')

    code = sp_oauth.parse_response_code(response)
    token_info = sp_oauth.get_access_token(code)

    token = token_info['access_token']

sp = spotipy.Spotify(auth=token)

要刷新我的令牌(每小时需要一次),我使用此功能.您何时何地调用它取决于您的程序.

To refresh my token (required every hour), I use this function. When and where you call it depends on your program.

def refresh():
    global token_info, sp

    if sp_oauth.is_token_expired(token_info):
        token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
        token = token_info['access_token']
        sp = spotipy.Spotify(auth=token)

这篇关于Spotipy使用授权码流程刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆