Spotify 刷新令牌 Python
Spotify Refresh Token Python
我有一个 python 程序,可以 returns 我目前正在听的任何歌曲。我从 Spotify API 获得的访问令牌只持续一个小时,我很难找到一种简单的方法在我的代码中实现刷新令牌。
SPOTIFY_GET_CURRENT_TRACK_URL = 'https://api.spotify.com/v1/me/player'
SPOTIFY_ACCESS_TOKEN = 'BQBQMLkkLb6az9TRv-PDXFFcm4KHrofc67H12Uzs3FXaPWHETy4ivnC-WGu2Jg5wlZ_yhggGypiO0Gnv_QkNGkefTuCjNR_Tzjw4pv2W9f_9_D7-LfD2W5KVPzh2DxMmf9TNPpSn200D_Xd4AD8F_MeJc-DIlA'
#Method to return the info for the currently playing track
def get_current_track():
response = requests.get(
SPOTIFY_GET_CURRENT_TRACK_URL,
headers = {
"Authorization": f"Bearer {SPOTIFY_ACCESS_TOKEN}"
}
)
resp_json = response.json()
track_id = resp_json['item']['id']
track_name = resp_json['item']['name']
artists = resp_json['item']['artists']
artists_name = ', '.join(
[artist['name'] for artist in artists]
)
link = resp_json['item']['external_urls']['spotify']
current_track_info = {
"id" : track_id,
"name" : track_name,
"artists" : artists_name,
"link" : link
}
return current_track_info
如何使用 class 来保留令牌,然后在令牌过时时再次请求?像这样:
class SpotifyTracker:
TRACK_URL = 'https://api.spotify.com/v1/me/player'
def __init__(self):
self._token = self._set_token()
def _set_token(self):
# ... logic to set token
def _request_track_data(self):
return requests.get(
self.TRACK_URL,
headers={
"Authorization": f"Bearer {self._token}"
}
)
def get_current_track(self):
response = self._request_track_data()
if response.code == 401:
self._set_token()
response = self._request_track_data()
resp_json = response.json()
...
return current_track_info
这是一个很棒的 YouTube 教程,回答了我所有的问题
https://youtu.be/-FsFT6OwE1A
我有一个 python 程序,可以 returns 我目前正在听的任何歌曲。我从 Spotify API 获得的访问令牌只持续一个小时,我很难找到一种简单的方法在我的代码中实现刷新令牌。
SPOTIFY_GET_CURRENT_TRACK_URL = 'https://api.spotify.com/v1/me/player'
SPOTIFY_ACCESS_TOKEN = 'BQBQMLkkLb6az9TRv-PDXFFcm4KHrofc67H12Uzs3FXaPWHETy4ivnC-WGu2Jg5wlZ_yhggGypiO0Gnv_QkNGkefTuCjNR_Tzjw4pv2W9f_9_D7-LfD2W5KVPzh2DxMmf9TNPpSn200D_Xd4AD8F_MeJc-DIlA'
#Method to return the info for the currently playing track
def get_current_track():
response = requests.get(
SPOTIFY_GET_CURRENT_TRACK_URL,
headers = {
"Authorization": f"Bearer {SPOTIFY_ACCESS_TOKEN}"
}
)
resp_json = response.json()
track_id = resp_json['item']['id']
track_name = resp_json['item']['name']
artists = resp_json['item']['artists']
artists_name = ', '.join(
[artist['name'] for artist in artists]
)
link = resp_json['item']['external_urls']['spotify']
current_track_info = {
"id" : track_id,
"name" : track_name,
"artists" : artists_name,
"link" : link
}
return current_track_info
如何使用 class 来保留令牌,然后在令牌过时时再次请求?像这样:
class SpotifyTracker:
TRACK_URL = 'https://api.spotify.com/v1/me/player'
def __init__(self):
self._token = self._set_token()
def _set_token(self):
# ... logic to set token
def _request_track_data(self):
return requests.get(
self.TRACK_URL,
headers={
"Authorization": f"Bearer {self._token}"
}
)
def get_current_track(self):
response = self._request_track_data()
if response.code == 401:
self._set_token()
response = self._request_track_data()
resp_json = response.json()
...
return current_track_info
这是一个很棒的 YouTube 教程,回答了我所有的问题 https://youtu.be/-FsFT6OwE1A