47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import requests
|
|
import base64
|
|
# Method 2: Using regular expressions
|
|
|
|
import re
|
|
|
|
|
|
# Your Spotify app credentials
|
|
client_id = '3b4db45bb66b45e9a2532989c8034332'
|
|
client_secret = '4ced4e4575094b749834a60996680c6c'
|
|
|
|
# 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())
|
|
|