diff --git a/examples/app.py b/examples/app.py index 583d415..5b93c10 100644 --- a/examples/app.py +++ b/examples/app.py @@ -7,12 +7,20 @@ Prerequisites export SPOTIPY_CLIENT_ID=client_id_here export SPOTIPY_CLIENT_SECRET=client_secret_here export SPOTIPY_REDIRECT_URI='http://127.0.0.1:8080' // must contain a port - + // SPOTIPY_REDIRECT_URI must be added to your [app settings](https://developer.spotify.com/dashboard/applications) + OPTIONAL + // in development environment for debug output + export FLASK_ENV=development + // so that you can invoke the app outside of the file's directory include + export FLASK_APP=/path/to/spotipy/examples/app.py + // on Windows, use `SET` instead of `export` Run app.py python3 -m flask run --port=8080 + NOTE: If receiving "port already in use" error, try other ports: 5000, 8090, 8888, etc... + (will need to be updated in your Spotify app and SPOTIPY_REDIRECT_URI variable) """ import os @@ -31,6 +39,8 @@ caches_folder = './.spotify_caches/' if not os.path.exists(caches_folder): os.makedirs(caches_folder) +def session_cache_path(): + return caches_folder + session.get('uuid') @app.route('/') def index(): @@ -38,7 +48,10 @@ def index(): # Step 1. Visitor is unknown, give random ID session['uuid'] = str(uuid.uuid4()) - auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path(), show_dialog=True) + auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing playlist-modify-private', + cache_path=session_cache_path(), + show_dialog=True) + if request.args.get("code"): # Step 3. Being redirected from Spotify auth page auth_manager.get_access_token(request.args.get("code")) @@ -53,20 +66,26 @@ def index(): spotify = spotipy.Spotify(auth_manager=auth_manager) return f'