This repository was archived by the owner on May 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
44 lines (34 loc) · 1.48 KB
/
init.py
File metadata and controls
44 lines (34 loc) · 1.48 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
"""Reset auth starting from new authorisation code from Spotify."""
import requests
from sys import exit
from urllib.parse import urlencode
from config import client_id, client_secret, redirect_uri, https_proxy, use_proxy, verify_cert
from error import problem_check
def reinitialize(code):
"""Reset auth starting from new authorisation code from Spotify."""
if use_proxy == True:
proxy = {"https": https_proxy}
else:
proxy = None
data = {"grant_type": "authorization_code",
"code": code, "redirect_uri": redirect_uri}
try:
response = requests.post("https://accounts.spotify.com/api/token", proxies=proxy,
verify=verify_cert, auth=(client_id, client_secret), data=data)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
problem_check(response) # type: ignore
rj = response.json()
access_token = rj["access_token"]
refresh_token = rj["refresh_token"]
with open("access_token", 'w') as writer:
writer.write(access_token)
with open("refresh_token", 'w') as writer:
writer.write(refresh_token)
def user_auth():
"""Generate Spotify user auth request URI."""
scopes = "playlist-modify-private playlist-read-private"
user_auth_uri = "https://accounts.spotify.com/authorize?" + \
urlencode({"response_type": "code", "client_id": client_id,
"scope": scopes, "redirect_uri": redirect_uri})
print(user_auth_uri)