Spotipy错误401-未经授权,尽管使用的令牌是有效的,并且已经使用了
嘿,
我有点沮丧,因为我找不到错误。我尝试设置一个脚本,该脚本将我的播放列表详细介绍为由于报告滥用情况的方式。我正在使用“ playlist_change_details”和“ 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 Auth代码!试图获得有效的访问令牌...
可用访问令牌!试图获取用户信息...
HTTP Error for PUT to https://api.spotify.com/v1/playlists/XXXXXXXXXXXXXX /images 带有参数:{}由于未经授权而返回401。
上传新的封面图像失败了。播放列表的详细信息已更改。
http错误中的消息:
{
"error": {
"status": 401,
"message": "No token provided"
}
}
谢谢您的宝贵时间!
问候
Mrschiller
Heyo Guys,
I am kinda frustraded because I can't find the error. I try to setup a script, which sets my playlists details back to how it was because of report abuses. I am using 'playlist_change_details' and 'playlist_upload_cover_image' which should use the same scope. Changing the name and the description is working, but the cover image tells me it got no token provided, although they use the same requested token. The image itself is valid and I didn't get an error about that.
Authentication Part (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
Changing Details Part (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.')
Console Output:
#XXXXXXXXXXXXXX
Found Spotify auth code in Request URL! Trying to get valid access token...
Access token available! Trying to get user information...
HTTP Error for PUT to https://api.spotify.com/v1/playlists/XXXXXXXXXXXXXX/images with Params: {} returned 401 due to Unauthorized.
Uploading a new cover image failed. The details of the playlist got changed.
Message in the HTTP Error:
{
"error": {
"status": 401,
"message": "No token provided"
}
}
Thank you for your time!
Greets
MrSchiller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看用于授权范围的文档您也可能需要包括
ugc-image-upload
的范围。Looking at the documentation for authorization scopes, you may also need to include the scope for
ugc-image-upload
.