-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrefresh_token_gen.py
52 lines (38 loc) · 1.18 KB
/
refresh_token_gen.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
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, request, redirect
import requests
from dotenv import load_dotenv
import os
import urllib
app = Flask(__name__)
load_dotenv()
# Client Keys
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
# Server-side
port = 8888
redirect_uri = f"http://localhost:8888/callback"
scope = "playlist-read-private playlist-read-collaborative user-library-read user-follow-read"
@app.route("/")
def index():
auth_url = "https://accounts.spotify.com/authorize?" + urllib.parse.urlencode({
"response_type": "code",
"redirect_uri": redirect_uri,
"scope": scope,
"client_id": CLIENT_ID
})
return redirect(auth_url)
@app.route("/callback/")
def callback():
code_payload = {
"grant_type": "authorization_code",
"code": str(request.args['code']),
"redirect_uri": redirect_uri,
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
}
resp = requests.post(
"https://accounts.spotify.com/api/token",
data=code_payload)
return resp.json()["refresh_token"]
if __name__ == "__main__":
app.run(debug=True, port=port, host="0.0.0.0")