import requests import base64 # Method 2: Using regular expressions import re # Your Spotify app credentials client_id = '***REDACTED***' client_secret = '***REDACTED***' # Encode client_id and client_secret in Base64 client_creds = f"{client_id}:{client_secret}" client_creds_b64 = base64.b64encode(client_creds.encode()) # Spotify token endpoint token_url = 'https://accounts.spotify.com/api/token' # Parameters for requesting access token token_data = { "grant_type": "client_credentials", "client_secret": client_secret, "client_id": client_id, "scope": "user-read-private, playlist-read-private, playlist-read-collaborative", } # HTTP headers including encoded client_id and client_secret token_headers = { # "Authorization": f"Basic {client_creds_b64.decode()}" "Content-Type" : "application/x-www-form-urlencoded" } # Requesting access token token_response = requests.post(token_url, data=token_data, headers=token_headers) # Handling response if token_response.status_code not in range(200, 299): print(f"Failed to retrieve access token: {token_response.status_code}") print(token_response.json()) token = token_response.json()['access_token'] token_type = token_response.json()['token_type'] token_time = token_response.json()['expires_in'] if __name__ == "__main__": print(token_response.json())