簡體   English   中英

Spotipy 刷新訪問令牌 Oauth2

[英]Spotipy Refreshing Access Token Oauth2

我正在嘗試制作一個從 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

更新您的 spipy 版本,令牌應該會自動刷新

pip3 install spotipy --upgrade

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM