-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_auth.py
48 lines (36 loc) · 1.33 KB
/
spotify_auth.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
## Run with
# export FLASK_APP=spotify_auth.py ; flask run
from flask import Flask, escape, request
from spotify_keys import *
import urllib
import requests
import base64
app = Flask(__name__)
server_ip="localhost" # 192.168.2.52
def request_token(code):
url = "https://accounts.spotify.com/api/token"
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirect_uri
}
b64auth = "{}:{}".format(client_id,client_secret)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic {}'.format(base64.b64encode(b64auth.encode("utf-8")).decode("utf-8"))
}
r = requests.post(url , headers=headers, data=data )
print("status code {}".format(r.status_code))
print("request_token {}".format(r.text))
@app.route('/callback')
def callback():
code = request.args.get('code')
request_token(code)
return "Done"
print("Oauth process started, click on this URL to begin auth")
scopes = 'user-read-private user-read-currently-playing user-modify-playback-state user-read-playback-state'
redirect_uri = "http://{}:5000/callback".format(server_ip)
auth_url = 'https://accounts.spotify.com/authorize?response_type=code&client_id={}&scope={}&redirect_uri={}'.format(
client_id , urllib.parse.quote(scopes), urllib.parse.quote(redirect_uri)
)
print(auth_url)