-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_refresh_token.py
More file actions
83 lines (73 loc) · 2.71 KB
/
get_refresh_token.py
File metadata and controls
83 lines (73 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Run this script to obtain a Spotify refresh token."""
import typing
import base64
import getpass
import json
import urllib.parse
import urllib.request
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer
class RequestHandler(BaseHTTPRequestHandler):
auth_code: str | None = None
def do_GET(self):
try:
values = urllib.parse.parse_qs(
urllib.parse.urlparse(self.path).query,
keep_blank_values=False,
).get("code", [])
RequestHandler.auth_code = values[0] if values else None
except Exception:
RequestHandler.auth_code = None
self.send_response(200 if RequestHandler.auth_code else 400)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(
b"<h2>Done! You can close this tab.</h2>"
if RequestHandler.auth_code
else b"<h2>Authorization failed.</h2>"
)
def log_message(self, format: str, *args: typing.Any):
pass
if __name__ == "__main__":
CLIENT_ID = input("Client ID: ").strip()
CLIENT_SECRET = getpass.getpass("Client Secret (hidden): ").strip()
REDIRECT_URI = "http://127.0.0.1:3000/callback"
auth_url = "https://accounts.spotify.com/authorize?" + urllib.parse.urlencode(
{
"response_type": "code",
"client_id": CLIENT_ID,
"scope": "user-read-currently-playing",
"redirect_uri": REDIRECT_URI,
}
)
print(
f"Opening browser for Spotify authorization...\nIf it does not open, visit:\n {auth_url}\n"
)
webbrowser.open(auth_url)
with HTTPServer(("127.0.0.1", 3000), RequestHandler) as server:
server.handle_request()
if not RequestHandler.auth_code:
raise SystemExit("Failed to capture authorization code.")
url = urllib.request.Request(
"https://accounts.spotify.com/api/token",
data=urllib.parse.urlencode(
{
"grant_type": "authorization_code",
"code": RequestHandler.auth_code,
"redirect_uri": REDIRECT_URI,
}
).encode(),
headers={
"Authorization": f"Basic {base64.b64encode(f'{CLIENT_ID}:{CLIENT_SECRET}'.encode()).decode()}",
"Content-Type": "application/x-www-form-urlencoded",
},
method="POST",
)
with urllib.request.urlopen(url, timeout=10) as response:
token_info = json.load(response)
if isinstance(token_info, dict) and (
refresh_token := token_info.get("refresh_token")
):
print(f"\nREFRESH_TOKEN:\n {refresh_token}")
else:
raise SystemExit("No refresh token in response.")