Мой полный код (примерный тестовый сценарий, в котором я жестко запрограммировал код перенаправления Spotify и секреты для выполнения запросов отслеживания):
Код: Выделить всё
import requests, json, time
from requests.auth import HTTPBasicAuth
class TokenManager:
current_refresh_token = ""
def __init__(self):
self.current_refresh_token = ""
def get_first_spotify_token(self):
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"redirect_uri": "",
"code": "",
"grant_type": "authorization_code"
}
token_request = requests.post(
url="https://accounts.spotify.com/api/token",
data=data,
headers=headers,
auth=HTTPBasicAuth("", "")
)
if (token_request.status_code == 200):
token_request_json = json.loads(token_request.text)
self.current_refresh_token = token_request_json["refresh_token"]
print(token_request_json)
return token_request_json
else:
print(token_request.status_code)
print(token_request.text)
def get_refresh_token(self):
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"client_id": "",
"client_secret": "",
"refresh_token": self.current_refresh_token,
"grant_type": "refresh_token"
}
token_request = requests.post(
url="https://accounts.spotify.com/api/token",
data=data,
headers=headers
)
if (token_request.status_code == 200):
token_request_json = json.loads(token_request.text)
print(token_request_json)
self.current_refresh_token = token_request_json["access_token"]
return token_request_json
else:
print(token_request.status_code)
print(token_request.text)
def get_tracks():
token_manager = TokenManager()
token = token_manager.get_first_spotify_token()
limit = 50
offset = 0
next = ""
total = 1000000 # Super large int
while (offset