Spotipy刷新访问令牌Oauth2

问题描述 投票:1回答:1

我正在尝试制作一个程序,以从Spotify播放列表中删除歌曲。尽管我将身份验证令牌保存在我的硬盘驱动器上并在制作oauth对象时指定了缓存路径,但我目前无法刷新它们。

from bottle import route, run, request
import spotipy
from spotipy import oauth2
import time

PORT_NUMBER = 8080
SPOTIPY_CLIENT_ID = 'e1bb48ed8b594aeb9faf74f7e8915de7'
SPOTIPY_CLIENT_SECRET = 'dd6de8b7a0324d6ebf1fc0591e8e3220'
SPOTIPY_REDIRECT_URI = 'http://localhost:8080'
SCOPE = 'playlist-modify-public'
tracks = ['6DCZcSspjsKoFjzjrWoCdn']



def remove(sp_oauth):

    access_token = ""

    token_info = sp_oauth.get_cached_token()

    if token_info:
        if sp_oauth.is_token_expired:
            token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
            access_token = token_info['access_token']
        else:
            print ("Found cached token!")
            access_token = token_info['access_token']

    if access_token:
        print ("Access token available! Trying to get user information...")
        sp = spotipy.Spotify(access_token)
        results = sp.current_user_playlists()
        userinfo = sp.current_user()
        userid = userinfo['id']
        for items in results['items']:
            if userid == items['owner']['id']:
                sp.user_playlist_remove_all_occurrences_of_tracks(items['owner']['id'],items['id'],tracks)
                print("removed from " + items['owner']['display_name'] + " s list  " + items['name'])




example =oauth2.SpotifyOAuth( SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET,SPOTIPY_REDIRECT_URI,scope=SCOPE,cache_path=r'C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\Caches\example')

remove(example)

我得到的错误是

Traceback (most recent call last):
  File "C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\unfollowLists.py", line 212, in <module>
    remove(blake)
  File "C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\unfollowLists.py", line 63, in remove
    if sp_oauth.is_token_expired:
AttributeError: 'SpotifyOAuth' object has no attribute 'is_token_expired'

我拥有的Spotipy版本是2.4.4

python spotipy
1个回答
0
投票

更新您的Spotipy版本,令牌应自动刷新

pip3 install spotipy --upgrade
© www.soinside.com 2019 - 2024. All rights reserved.