我正在尝试了解 Spotipy 库以创建一些供个人使用的程序。最初我尝试使用“客户端凭证流”访问 Spotify,但由于“授权代码流”似乎更适合我目前关注的用例。
我相信我已经设法实现了检查是否有缓存令牌的基本流程。如果没有任何缓存的令牌,尝试获取新令牌并缓存新令牌。
问题是当我尝试获取当前正在播放的歌曲或用户信息时,API 返回“仅支持有效的承载身份验证”错误。我知道 Spotify API 使用无效令牌或不记名令牌不具有所需范围来触发此异常。但仍然找不到问题。
My Client ID、Client Secret 和 Redirect URI 定义为环境变量。
您可以在下面找到代码及其输出。 (代码中可能有一些非 Python 风格的使用,我愿意接受任何可以提高我的 Python 技能的建议。)
代码:
import spotipy
from spotipy import oauth2
from spotipy import cache_handler
import requests
import json
from pprint import pprint
oAuthscope = "user-read-email,user-read-private,user-library-read,user-read-playback-state,user-modify-playback-state,user-read-currently-playing"
Sp_Cache = cache_handler.CacheFileHandler()
auth_manager = spotipy.SpotifyOAuth(scope=oAuthscope,cache_handler=Sp_Cache)
Sp_oAuth = spotipy.Spotify(auth_manager)
def init_Spotify_Session():
#Define access_token variable
access_token = ""
print ("\n Access_Token variable defined!")
#Try to get cached token
print ("\n Trying to get cached token!")
Token_Data = Sp_Cache.get_cached_token()
if Token_Data is not None:
print ("\n Cached Token = \n")
pprint (Token_Data )
print ("\n\n\n")
else :
print ("There is no cached token!!! Token cannot be printed!!")
if not Token_Data:
print ("There is not any cached token!")
authorisation_url = auth_manager.get_authorize_url()
print ("\n Authorisation URL retrieved = " + authorisation_url)
print ("\n Trying to get authorisation response:")
oAuth_Response = auth_manager.get_auth_response()
print ("\n oAuthResponse= ")
print (type(oAuth_Response))
print("\n oAuth_Response = " + oAuth_Response + "\n")
#Sp_Cache.save_token_to_cache(oAuth_Response)
#auth_Token = auth_manager.get_authorization_code(oAuth_Response)
#print ("\n CODE = \n" + auth_Token + "\n")
print ("\n\n\n Trying to get new access token!!! \n\n\n")
Token_Data = auth_manager.get_access_token(code=oAuth_Response)
access_token = Token_Data["access_token"]
print ("\n Access Token = \n")
pprint (Token_Data)
print ("\n\n\n")
pprint (access_token)
print ("\n\n\n")
Sp_Cache.save_token_to_cache(Token_Data)
print("\n Cached New Token = ")
print(Sp_Cache.get_cached_token()),print ("\n\n\n")
def Get_Current_Song():
global song_current_track, artist_current_track, cover_art_url
try:
response_current_track = Sp_oAuth.current_user_playing_track() # Gets response of currently playing track
song_current_track = response_current_track["item"]["name"]
artist_current_track = response_current_track["item"]["album"]["artists"][0]["name"]
cover_art_url = response_current_track ["item"]["album"]["images"][0]["url"]
#cover_art = urllib.request.urlretrieve(cover_art_url, "downloaded.jpg")
cover_art = requests.get(cover_art_url, "downloaded.jpg") ##gpt suggestion
#cover_art = Image.open("downloaded.jpg")
#cover_art.show()
print("cover art downloaded!!!")
#pprint(response_current_track) #prints whole response json
print("**************************")
print("Currently Playing:")
pprint(song_current_track) #prints current song title
pprint(artist_current_track) #prints current song artist
print("**************************")
except TypeError:
print(" ")
print ("Error Occured Details Below:")
print(" ")
print("!!!Check if spotify playing!!!")
print(" ")
#traceback.print_exc()
return song_current_track, artist_current_track, cover_art, cover_art_url
#Get_Current_Song(
def Get_Queued_Song():
global song_next_track, artist_next_track
try:
response_queued_track = Sp_oAuth.queue() #print(json.dumps(results, indent=4)) #Prints out json of
#print(list(response_queued_track.keys()))
#print("____________________________________________")
song_next_track = response_queued_track["queue"][0]["name"]
artist_next_track = response_queued_track["queue"][0]["artists"][0]["name"]
print("**************************")
print("Coming on Next:")
pprint(song_next_track) #prints current song title
pprint(artist_next_track) #prints current song artist
print("**************************")
except:
print(" ")
print ("Error Occured Details Below:")
print(" ")
print("!!!Check if spotify queue is empty!!!")
print(" ")
#traceback.print_exc()
return song_next_track, artist_next_track
init_Spotify_Session()
Get_Current_Song()
输出:
Access_Token variable defined!
Trying to get cached token!
Cached Token =
{'access_token': 'BQBVdYQaXUI24UZ_f_ttkSYWtRQWc0MGq3zXryEwtYBCBFLUH5AxC-aOnlmz8FmlqHZ7xveU7VeI95whGeitSb6iVuGAydalYcSaDwXFZwxRn24GZpUYYSji09QXaTKSzcTgFV_HtmS3hylvthXM9hAI6AIks1iLA4aU2axT1BjBToiQVagEzqq4pUkgaxHlfLgaIgmTMHe0Lq0mQTTI2Q',
'expires_at': 1680096145,
'expires_in': 3600,
'refresh_token': 'AQCXYYFHq3Xs-DYHsWZHq-TnDAW1OZ-W8TaYqZdSV6LBlJQH6VYqnwrYuEKxy4nsMoMaLHGHxxb8Vf4LZjnkm3E4TRq1QtZ3n5vFg7mZLzTdFaEgPiZhkVBVyRIEL5yW3TY',
'scope': 'user-library-read user-modify-playback-state '
'user-read-currently-playing user-read-email '
'user-read-playback-state user-read-private',
'token_type': 'Bearer'}
HTTP Error for GET to https://api.spotify.com/v1/me/player/currently-playing with Params: {} returned 400 due to Only valid bearer authentication supported
Traceback (most recent call last):
File "C:\Users\erensan\AppData\Roaming\Python\Python310\site-packages\spotipy\client.py", line 269, in _internal_call
response.raise_for_status()
File "C:\Users\erensan\AppData\Roaming\Python\Python310\site-packages\requests\models.py", line
1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/me/player/currently-playing
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\erensan\Desktop\Python Dev\Spotipy\oAuth\Spotipy_oAuth_eren2.py", line 141, in <module>
Get_Current_Song()
File "C:\Users\erensan\Desktop\Python Dev\Spotipy\oAuth\Spotipy_oAuth_eren2.py", line 78, in Get_Current_Song
response_current_track = Sp_oAuth.current_user_playing_track() # Gets response of currently playing track
File "C:\Users\erensan\AppData\Roaming\Python\Python310\site-packages\spotipy\client.py", line 1230, in current_user_playing_track
return self._get("me/player/currently-playing")
File "C:\Users\erensan\AppData\Roaming\Python\Python310\site-packages\spotipy\client.py", line 321, in _get
return self._internal_call("GET", url, payload, kwargs)
File "C:\Users\erensan\AppData\Roaming\Python\Python310\site-packages\spotipy\client.py", line 291, in _internal_call
raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/me/player/currently-playing:
Only valid bearer authentication supported, reason: None
PS C:\Users\erensan\Desktop\Python Dev\Spotipy\oAuth>
我已经尝试清除缓存的代码,并向 auth_manager 添加额外的范围,但结果是一样的。
你错过了两个项目
#1 需要为
spotipy
设置环境变量
os.environ["SPOTIPY_CLIENT_ID"] = "<your client ID>"
os.environ["SPOTIPY_CLIENT_SECRET"] = "<your client Secret>"
os.environ["SPOTIPY_REDIRECT_URI"] = "<your App's registered redirect URI>"
#2 Spotify() 类使用输入参数正确分配初始化
来自
spotipy.Spotify(auth_manager)
到
spotipy.Spotify(auth_manager=auth_manager)
演示代码
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from spotipy import cache_handler
import os
from pprint import pprint
# Set your Spotify API credentials as environment variables, it will pick by spotipy API
os.environ["SPOTIPY_CLIENT_ID"] = "<your client ID>"
os.environ["SPOTIPY_CLIENT_SECRET"] = "<your client Secret>"
os.environ["SPOTIPY_REDIRECT_URI"] = "<your App's registered redirect URI>"
# multiple scopes
oAuthscope = "user-read-email,user-read-private,user-library-read,user-read-playback-state,user-modify-playback-state,user-read-currently-playing"
Sp_Cache = cache_handler.CacheFileHandler()
auth_manager = spotipy.SpotifyOAuth(scope=oAuthscope,cache_handler=Sp_Cache)
Sp_oAuth = spotipy.Spotify(auth_manager=auth_manager)
def init_Spotify_Session():
access_token = ""
print ("\n Access_Token variable defined!")
Token_Data = Sp_Cache.get_cached_token()
if Token_Data is not None:
print ("\n Cached Token = \n")
pprint (Token_Data )
print ("\n")
else :
print ("There is no cached token!!! Token cannot be printed!!")
if not Token_Data:
print ("There is not any cached token!")
authorisation_url = auth_manager.get_authorize_url()
print ("\n Authorisation URL retrieved = " + authorisation_url)
print ("\n Trying to get authorisation response:")
oAuth_Response = auth_manager.get_auth_response()
print ("\n oAuthResponse= ")
print (type(oAuth_Response))
print("\n oAuth_Response = " + oAuth_Response + "\n")
print ("\n Trying to get new access token!!! \n")
Token_Data = auth_manager.get_access_token(code=oAuth_Response)
access_token = Token_Data["access_token"]
print ("\n Access Token = \n")
pprint (Token_Data)
pprint (access_token)
Sp_Cache.save_token_to_cache(Token_Data)
print("\n Cached New Token = ")
print(Sp_Cache.get_cached_token()),print ("\n")
def Get_Current_Song():
response_current_track = Sp_oAuth.current_user_playing_track()
if response_current_track is not None and response_current_track["item"] is not None:
song = response_current_track["item"]
print(song['artists'][0]['name'], " – ", song['name'])
init_Spotify_Session()
Get_Current_Song()
你也不需要自己处理令牌。
spotipy
适合你。
如果不存在.cache
,将启动浏览器并登录spotify
以获取令牌。
如果退出'.cache
, then skip login and pick up the token from
.cache`文件。
.cache
将包括六个信息
(access_token
, token_type
, expires_in
, refresh_token
, scope
和 expires_at
)
这个简单的代码
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os
# Set your Spotify API credentials as environment variables, it will pick by spotipy API
os.environ["SPOTIPY_CLIENT_ID"] = "<your client ID>"
os.environ["SPOTIPY_CLIENT_SECRET"] = "<your client Secret>"
os.environ["SPOTIPY_REDIRECT_URI"] = "<your App's registered redirect URI>"
# multiple scopes
oAuthscope = "user-read-email,user-read-private,user-library-read,user-read-playback-state,user-modify-playback-state,user-read-currently-playing"
Sp_oAuth = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=oAuthscope))
def Get_Current_Song():
response_current_track = Sp_oAuth.current_user_playing_track()
if response_current_track is not None and response_current_track["item"] is not None:
song = response_current_track["item"]
print(song['artists'][0]['name'], " – ", song['name'])
Get_Current_Song()