47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import requests
|
|
import webbrowser
|
|
from urllib.parse import urlencode, urlparse, parse_qs
|
|
import base64
|
|
|
|
|
|
CLIENT_ID = input("YOUR_CLIENT_ID: ")
|
|
CLIENT_SECRET = input("YOUR_CLIENT_SECRET: ")
|
|
BASE_64 = base64.b64encode(f"{CLIENT_ID}:{CLIENT_SECRET}".encode()).decode()
|
|
REDIRECT_URI = "http://127.0.0.1:8888/callback"
|
|
SCOPE = "user-read-private user-read-email"
|
|
|
|
webbrowser.open(
|
|
"https://accounts.spotify.com/authorize?"
|
|
+ urlencode(
|
|
{
|
|
"client_id": CLIENT_ID,
|
|
"response_type": "code",
|
|
"redirect_uri": REDIRECT_URI,
|
|
"scope": SCOPE,
|
|
}
|
|
)
|
|
)
|
|
|
|
url = input("Paste FULL callback URL: ").strip()
|
|
code = parse_qs(urlparse(url).query)["code"][0]
|
|
|
|
r = requests.post(
|
|
"https://accounts.spotify.com/api/token",
|
|
auth=(CLIENT_ID, CLIENT_SECRET),
|
|
data={
|
|
"grant_type": "authorization_code",
|
|
"code": code,
|
|
"redirect_uri": REDIRECT_URI,
|
|
},
|
|
)
|
|
r.raise_for_status()
|
|
tokens = r.json()
|
|
|
|
print("-" * 20)
|
|
print("ACCESS TOKEN :", tokens["access_token"])
|
|
print("REFRESH TOKEN:", tokens["refresh_token"])
|
|
print(
|
|
"BASE64:", BASE_64
|
|
) # TODO set these automatically in config.yaml, auto update when expired
|
|
print("-" * 20)
|