Spotipy ERROR 401 -未经授权,尽管使用的令牌是有效的,并且之前已经使用过



嘿,伙计们,
我有点沮丧,因为我找不到错误。我试着设置一个脚本,它将我的播放列表的细节设置回由于报告滥用的情况。我使用的是"playlist_change_details"one_answers"playlist_upload_cover_image",应该使用相同的范围。更改名称和描述正在工作,但封面图像告诉我它没有提供令牌,尽管他们使用相同的请求令牌。图片本身是有效的,我没有得到一个错误。

认证部分(TaskSpotifyAuth.py):

import spotipy
import spotipy.oauth2 as oauth2
def auth(SCOPE = None, debug = False):
if SCOPE != None:
sp_oauth = oauth2.SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, scope=SCOPE,
redirect_uri=SPOTIPY_REDIRECT_URI, cache_path=CACHE)
access_token = ""
token_info = sp_oauth.get_cached_token()
if token_info != None:
if token_info['scope'] == SCOPE and sp_oauth.validate_token(token_info):
print("Found cached token for the scope: <" + SCOPE + ">") if debug == True else None
access_token = token_info['access_token']
else:
url = sp_oauth.get_authorization_code()
print(url)
code = sp_oauth.parse_response_code(url)
if code != "":
print("Found Spotify auth code in Request URL! Trying to get valid access token...") if debug == True else None
token_info = sp_oauth.get_access_token(code=code)
access_token = token_info['access_token']
if access_token:
print("Access token available! Trying to get user information...") if debug == True else None
sp = spotipy.Spotify(access_token)
return sp
else:
return None

更改详细信息部分(TaskSpotify.py):

import TaskSpotifyAuth
def SpotifyLogin(scope=None, debug=False):
if scope == None:
return TaskSpotifyAuth.noPreToken()
else:
return TaskSpotifyAuth.auth(SCOPE=scope, debug=debug)
def change_playlist_details(pl_id, pl_name, pl_desc, pl_cover, debug=False):
sp = SpotifyLogin(scope='playlist-modify-public', debug=debug)
try:
sp.playlist_change_details(playlist_id=pl_id, name=pl_name, description=pl_desc)
try:
sp.playlist_upload_cover_image(playlist_id=pl_id, image_b64=pl_cover)
except:
print('Uploading a new cover image failed. The details of the playlist got changed.')
except:
print('Changing of the playlist details failed. No changes were made.')

控制台输出:
#XXXXXXXXXXXXXX
在请求URL中找到Spotify授权代码!试图获得有效的访问令牌…
访问令牌可用!试图获取用户信息…
使用参数访问https://api.spotify.com/v1/playlists/XXXXXXXXXXXXXX/images时出现HTTP错误:{}由于未授权返回401。
上传新封面图片失败。播放列表的细节被更改。

HTTP错误消息:

{
"error": {
"status": 401,
"message": "No token provided"
}
}

感谢您的宝贵时间!
MrSchiller

查看授权范围的文档,您可能还需要包括ugc-image-upload的范围。

最新更新