-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspotipy_oauth_demo.py
54 lines (41 loc) · 1.56 KB
/
spotipy_oauth_demo.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
53
# https://github.com/plamere/spotipy/blob/master/spotipy/util.py
# http://www.acmesystems.it/python_httpd
from bottle import route, run, request
import spotipy
from spotipy import oauth2
PORT_NUMBER = 8080
SPOTIPY_CLIENT_ID = '6bdd6d1ebf3747a99b78ccd3938e9b75'
SPOTIPY_CLIENT_SECRET = '7023ea1048b6437784880f05d37708f5'
SPOTIPY_REDIRECT_URI = 'http://localhost:8080'
SCOPE = 'user-library-read'
CACHE = '.spotipyoauthcache'
sp_oauth = oauth2.SpotifyOAuth( SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET,SPOTIPY_REDIRECT_URI,scope=SCOPE,cache_path=CACHE )
@route('/')
def index():
access_token = ""
token_info = sp_oauth.get_cached_token()
if token_info:
print("Found cached token!")
access_token = token_info['access_token']
else:
url = request.url
code = sp_oauth.parse_response_code(url)
if code != url:
print("Found Spotify auth code in Request URL! Trying to get valid access token...")
token_info = sp_oauth.get_access_token(code)
access_token = token_info['access_token']
if access_token:
print("Access token available! Trying to get user information...")
sp = spotipy.Spotify(access_token)
results = sp.current_user()
return results
else:
return htmlForLoginButton()
def htmlForLoginButton():
auth_url = getSPOauthURI()
htmlLoginButton = "<a href='" + auth_url + "'>Login to Spotify</a>"
return htmlLoginButton
def getSPOauthURI():
auth_url = sp_oauth.get_authorize_url()
return auth_url
run(host='', port=8080)