-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_refresh_token.py
41 lines (30 loc) · 1.19 KB
/
get_refresh_token.py
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
# Script to get refresh token from Spotify
import base64
import urllib.parse
import requests
from config import CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, SCOPES
authorization_url = f"https://accounts.spotify.com/authorize?client_id={CLIENT_ID}&response_type=code&redirect_uri={REDIRECT_URI}&scope={urllib.parse.quote_plus(SCOPES)}"
print()
print(authorization_url)
print()
print("Open url in the browser for authorization")
print()
print(
"After authorization, Spotify will redirect you to another URL with authorization code. Copy the authorization code from URL and paste it below to get access token. Copy all tetters after `code=` in URL string"
)
print()
authorization_code = input("Authorization code: ")
auth_header = f"{CLIENT_ID}:{CLIENT_SECRET}"
auth_header_b64 = base64.b64encode(auth_header.encode("ascii")).decode("ascii")
headers = {"Authorization": f"Basic {auth_header_b64}"}
data = {
"grant_type": "authorization_code",
"code": authorization_code,
"redirect_uri": REDIRECT_URI,
}
response = requests.post(
"https://accounts.spotify.com/api/token", headers=headers, data=data
)
response_data = response.json()
print()
print(f"Refresh token: {response_data['refresh_token']}")