-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_auth.py
48 lines (35 loc) · 1.43 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
48
from os import error
from pyspotify.auth.authenticate import _refresh_access_token
from pyspotify.auth.auth_config import AuthMode
from urllib.parse import urlencode
import requests
import json
from flask import Flask, render_template, url_for, redirect,request
from pyspotify.core import read_config_file, BadRequestError
from pyspotify.auth import _authentication_request, Authorization, _get_access_token, _authorization_code_request
app = Flask(__name__)
config = read_config_file()
@app.route('/')
def home():
params = {
'response_type' : 'code',
'client_id' : config.client_id,
'redirect_uri' : 'http://localhost:5000/callback',
'scope' : 'user-read-private playlist-modify-private user-read-recently-played user-top-read user-library-modify user-library-read',
}
encoded_url = urlencode(params)
url = f'{config.AUTH_URL}?{encoded_url}'
return render_template('index.html', url=url)
@app.route('/callback')
def callback():
code = request.args.get('code', '')
response = _authorization_code_request(code)
try:
with open('.pyspotify', mode='w', encoding='utf-8') as auth_code_file:
auth_code_file.write(response.refresh_token)
except error as err:
raise BadRequestError(err)
finally:
return f'All set. Please close the window.'
if __name__ == '__main__':
app.run(host='localhost', port=5000)