mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-10-11 17:10:45 +00:00
Add FlaskSessionCacheHandler (#833)
Updated examples/app.py Updated CHANGELOG.md and appropriate docs.
This commit is contained in:
1 parent
7337bf9352
commit
7fc08809f0
4 files changed
+40
-26
No files matched your search
+12
-26
@@ -27,7 +27,6 @@ import os
|
||||
from flask import Flask, session, request, redirect
|
||||
from flask_session import Session
|
||||
import spotipy
|
||||
import uuid
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = os.urandom(64)
|
||||
@@ -35,57 +34,44 @@ app.config['SESSION_TYPE'] = 'filesystem'
|
||||
app.config['SESSION_FILE_DIR'] = './.flask_session/'
|
||||
Session(app)
|
||||
|
||||
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():
|
||||
if not session.get('uuid'):
|
||||
# Step 1. Visitor is unknown, give random ID
|
||||
session['uuid'] = str(uuid.uuid4())
|
||||
|
||||
cache_handler = spotipy.cache_handler.CacheFileHandler(cache_path=session_cache_path())
|
||||
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
|
||||
auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing playlist-modify-private',
|
||||
cache_handler=cache_handler,
|
||||
show_dialog=True)
|
||||
cache_handler=cache_handler,
|
||||
show_dialog=True)
|
||||
|
||||
if request.args.get("code"):
|
||||
# Step 3. Being redirected from Spotify auth page
|
||||
# Step 2. Being redirected from Spotify auth page
|
||||
auth_manager.get_access_token(request.args.get("code"))
|
||||
return redirect('/')
|
||||
|
||||
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
||||
# Step 2. Display sign in link when no token
|
||||
# Step 1. Display sign in link when no token
|
||||
auth_url = auth_manager.get_authorize_url()
|
||||
return f'<h2><a href="{auth_url}">Sign in</a></h2>'
|
||||
|
||||
# Step 4. Signed in, display data
|
||||
# Step 3. Signed in, display data
|
||||
spotify = spotipy.Spotify(auth_manager=auth_manager)
|
||||
return f'<h2>Hi {spotify.me()["display_name"]}, ' \
|
||||
f'<small><a href="/sign_out">[sign out]<a/></small></h2>' \
|
||||
f'<a href="/playlists">my playlists</a> | ' \
|
||||
f'<a href="/currently_playing">currently playing</a> | ' \
|
||||
f'<a href="/current_user">me</a>' \
|
||||
f'<a href="/current_user">me</a>' \
|
||||
|
||||
|
||||
|
||||
@app.route('/sign_out')
|
||||
def sign_out():
|
||||
try:
|
||||
# Remove the CACHE file (.cache-test) so that a new user can authorize.
|
||||
os.remove(session_cache_path())
|
||||
session.clear()
|
||||
except OSError as e:
|
||||
print ("Error: %s - %s." % (e.filename, e.strerror))
|
||||
session.pop("token_info", None)
|
||||
return redirect('/')
|
||||
|
||||
|
||||
@app.route('/playlists')
|
||||
def playlists():
|
||||
cache_handler = spotipy.cache_handler.CacheFileHandler(cache_path=session_cache_path())
|
||||
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
|
||||
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
|
||||
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
||||
return redirect('/')
|
||||
@@ -96,7 +82,7 @@ def playlists():
|
||||
|
||||
@app.route('/currently_playing')
|
||||
def currently_playing():
|
||||
cache_handler = spotipy.cache_handler.CacheFileHandler(cache_path=session_cache_path())
|
||||
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
|
||||
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
|
||||
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
||||
return redirect('/')
|
||||
@@ -109,7 +95,7 @@ def currently_playing():
|
||||
|
||||
@app.route('/current_user')
|
||||
def current_user():
|
||||
cache_handler = spotipy.cache_handler.CacheFileHandler(cache_path=session_cache_path())
|
||||
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
|
||||
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
|
||||
if not auth_manager.validate_token(cache_handler.get_cached_token()):
|
||||
return redirect('/')
|
||||
|
||||
Reference in new issue
Block a user