Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Spotipy使用授权代码流刷新令牌_Python_Authorization_Refresh_Spotipy - Fatal编程技术网

Python Spotipy使用授权代码流刷新令牌

Python Spotipy使用授权代码流刷新令牌,python,authorization,refresh,spotipy,Python,Authorization,Refresh,Spotipy,我有一个使用spotipy的长时间运行的脚本。一小时后(根据Spotify API),我的访问令牌过期。我成功地捕捉到了这一点,但我不知道从那里可以走到哪里才能真正刷新令牌。我使用的是授权代码流,而不是客户端凭据。以下是我授权的方式: token = util.prompt_for_user_token(username,scope=scopes,client_id=client_id,client_secret=client_secret, redirect_uri=redirect_uri)

我有一个使用spotipy的长时间运行的脚本。一小时后(根据Spotify API),我的访问令牌过期。我成功地捕捉到了这一点,但我不知道从那里可以走到哪里才能真正刷新令牌。我使用的是授权代码流,而不是客户端凭据。以下是我授权的方式:

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\u access\u token()
),文档列表仅用作刷新令牌的方法。我的理解是,对于授权代码流,您不需要使用
oauth
对象(因为您使用
prompt\u对用户令牌()进行身份验证)。如果是这种情况,我如何刷新我的令牌?

在上没有收到响应后,我觉得如果不使用OAuth2,就无法刷新令牌。这与以下内容相违背:

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

他们的授权代码流示例使用提示\用户\令牌()

我切换到OAuth方法,这是一个痛苦的过程,因为每次运行程序时都需要重新授权(实际上,这只是测试时的一个问题,但仍然是一个问题)。由于Spotipy文档中没有OAuth2的示例,我将把我的粘贴到这里

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)
要刷新我的令牌(每小时需要一次),我使用此函数。何时何地调用它取决于您的程序

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)