2

I am trying to make a spotify web app using Spotipy and Spotify's API, the barebone of the program uses the core of this program: https://github.com/plamere/spotipy/blob/master/examples/app.py such as the login and authentication page. I've added many other functions that I wanted to add and was able to deploy the web app using Heroku, my issues is that when another user tries to login in the sign up screen, they are immediately met with a Internal Server Error. I'm thinking it has to do with the tokens for users but this is my first time programming with an API and I'm not sure how I can go about solving this problem.

Here is the route function for the login

app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(64)
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('/', methods=['GET', 'POST'])
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())
    auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing playlist-modify-public',
                                                cache_handler=cache_handler,
                                                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"))
        return redirect('/')

    if not auth_manager.validate_token(cache_handler.get_cached_token()):
        # Step 2. Display sign in link when no token
        auth_url = auth_manager.get_authorize_url()
        return f'<h2 style="text-align: center"><a href="{auth_url}">Sign in</a></h2>'

    # Step 4. 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="/artist_rec">recommendation playlist</a> | ' \
           f'<a href="/current_user">me</a>' \

0 Answers0