刷新令牌 Spotify API
我有以下 python 程序,它从输入中传递的收听历史记录开始,提取各种信息,例如声学性、乐器性等……我遇到的问题是,当我传递大量收听历史记录时,程序崩溃,提示令牌已过期。阅读 Spotify 文档后,我发现防止这种情况发生的一种方法是使用授权代码流程,但我不太明白如何在我的代码中实现它。有人可以帮助我吗?
token = util.prompt_for_user_token(username=config.username,
scope=config.scope,
client_id=config.client_id,
client_secret=config.client_secret,
redirect_uri=config.redirect_uri)
# Now you can finally authorize your app. Once you click on Agree,
# you will be taken to the Redirect URI, which may well be a nonexistent page.
# Just copy the address and paste it in your Python console.
#print(token)
# write the function to get track_id
def get_id(track_name: str,artist:str, token: str) -> str:
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer ' + token,
}
track_artist = track_name+ " " + artist
params = [
('q',track_artist ),#q is the search query parameter
('type', 'track'),
]
try:
response = requests.get('https://api.spotify.com/v1/search',
headers = headers, params = params, timeout = 10)
json = response.json()
first_result = json['tracks']['items'][0]
track_id = first_result['id']
return track_id
except:
return None
# Get track_id for streaming history
spotify_data["track_id"] = spotify_data.apply(lambda x: get_id(x["track_name"],
x["artist_name"],
token),axis=1)
# get track's feature
my_feature = pd.DataFrame(columns=["song_id","energy", "liveness","tempo","speechiness",
"acousticness","instrumentalness","danceability",
"duration_ms","loudness","valence",
"mode","key"])
# filter the streaming data by non-null result
spotify_data_nonull = spotify_data["track_id"].dropna()
#print(spotify_data_nonull)
track = list(OrderedDict.fromkeys(spotify_data_nonull))
#print(track)
sp = spotipy.Spotify(auth=token)
for song in track:
features = sp.audio_features(tracks = [song])[0]
if features is not None:
my_feature = my_feature.append({"song_id":song,
"energy":features['energy'],
"liveness":features['liveness'],
"tempo":features['tempo'],
"speechiness":features['speechiness'],
"acousticness":features['acousticness'],
"instrumentalness":features['instrumentalness'],
"danceability":features['danceability'],
"duration_ms":features['duration_ms'],
"loudness":features['loudness'],
"valence":features['valence'],
"mode":features['mode'],
"key":features["key"],
},ignore_index=True)
else:
pass
I have the following python program that starting from the listening history passed in input to extract various information such as acousticness, instrumentalness etc ... the problem I have is when I pass a large listening history the program crashes saying that the token has expired . Reading the spotify documentation I saw that one way to prevent this from happening is to use the Authorization Code Flow but I don't quite understand how to implement it in my code. Can anyone help me?
token = util.prompt_for_user_token(username=config.username,
scope=config.scope,
client_id=config.client_id,
client_secret=config.client_secret,
redirect_uri=config.redirect_uri)
# Now you can finally authorize your app. Once you click on Agree,
# you will be taken to the Redirect URI, which may well be a nonexistent page.
# Just copy the address and paste it in your Python console.
#print(token)
# write the function to get track_id
def get_id(track_name: str,artist:str, token: str) -> str:
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer ' + token,
}
track_artist = track_name+ " " + artist
params = [
('q',track_artist ),#q is the search query parameter
('type', 'track'),
]
try:
response = requests.get('https://api.spotify.com/v1/search',
headers = headers, params = params, timeout = 10)
json = response.json()
first_result = json['tracks']['items'][0]
track_id = first_result['id']
return track_id
except:
return None
# Get track_id for streaming history
spotify_data["track_id"] = spotify_data.apply(lambda x: get_id(x["track_name"],
x["artist_name"],
token),axis=1)
# get track's feature
my_feature = pd.DataFrame(columns=["song_id","energy", "liveness","tempo","speechiness",
"acousticness","instrumentalness","danceability",
"duration_ms","loudness","valence",
"mode","key"])
# filter the streaming data by non-null result
spotify_data_nonull = spotify_data["track_id"].dropna()
#print(spotify_data_nonull)
track = list(OrderedDict.fromkeys(spotify_data_nonull))
#print(track)
sp = spotipy.Spotify(auth=token)
for song in track:
features = sp.audio_features(tracks = [song])[0]
if features is not None:
my_feature = my_feature.append({"song_id":song,
"energy":features['energy'],
"liveness":features['liveness'],
"tempo":features['tempo'],
"speechiness":features['speechiness'],
"acousticness":features['acousticness'],
"instrumentalness":features['instrumentalness'],
"danceability":features['danceability'],
"duration_ms":features['duration_ms'],
"loudness":features['loudness'],
"valence":features['valence'],
"mode":features['mode'],
"key":features["key"],
},ignore_index=True)
else:
pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我重写了你代码的开头。当令牌过期时,spotipy 应自动生成/请求并使用刷新令牌。
PS 起初,您的代码向下滚动,没有看到代码的顶部。如果您使用我的代码,则可以删除代码中间的
sp = Spotipy.Spotify(auth=token)
。I rewrote the beginning of your code. The refresh token should be generated/requested and used automatically by spotipy when a token expires.
P.S. At first, your code was scrolled down and didn't see the top of your code. If you use my code, your
sp = spotipy.Spotify(auth=token)
in the middle of your code can be removed.