MusicIndexer/spotifyAuth.py
IlyaShurupov ee40573d9c initial
2024-07-08 23:18:07 +03:00

47 lines
1.3 KiB
Python

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())