[GH-ISSUE #924] Using Flask, RuntimeError: Working outside of request context. #553

Closed
opened 2026-02-27 23:23:18 +03:00 by kerem · 6 comments
Owner

Originally created by @Andrew3212 on GitHub (Dec 31, 2022).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/924

Trying to allow other people to get their current playing song with a Discord bot
Tried using this flask example below
https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py

this is my code

@bot.command
@lightbulb.command('c', 'Current Song')
@lightbulb.implements(lightbulb.PrefixCommand)
@app.route('/currently_playing')
async def currently_playing(ctx):
    cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
    auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing', cache_handler=cache_handler)
    if not auth_manager.validate_token(cache_handler.get_cached_token()):
        return redirect('/')
    sp = spotipy.Spotify(auth_manager=auth_manager)       
    song = sp.current_user_playing_track()

but i keep getting this error

raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
Originally created by @Andrew3212 on GitHub (Dec 31, 2022). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/924 Trying to allow other people to get their current playing song with a Discord bot Tried using this flask example below https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py this is my code ``` @bot.command @lightbulb.command('c', 'Current Song') @lightbulb.implements(lightbulb.PrefixCommand) @app.route('/currently_playing') async def currently_playing(ctx): cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session) auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing', cache_handler=cache_handler) if not auth_manager.validate_token(cache_handler.get_cached_token()): return redirect('/') sp = spotipy.Spotify(auth_manager=auth_manager) song = sp.current_user_playing_track() ``` but i keep getting this error ``` raise RuntimeError(unbound_message) from None RuntimeError: Working outside of request context. This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem. ```
kerem 2026-02-27 23:23:18 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@stephanebruckert commented on GitHub (Dec 31, 2022):

Do you know at what line the error is raised? This might be flask-related, not spotipy

<!-- gh-comment-id:1368199549 --> @stephanebruckert commented on GitHub (Dec 31, 2022): Do you know at what line the error is raised? This might be flask-related, not spotipy
Author
Owner

@Andrew3212 commented on GitHub (Dec 31, 2022):

if not auth_manager.validate_token(cache_handler.get_cached_token()):
I've never used flask before so i'm not sure whats wrong.

<!-- gh-comment-id:1368200835 --> @Andrew3212 commented on GitHub (Dec 31, 2022): `if not auth_manager.validate_token(cache_handler.get_cached_token()):` I've never used flask before so i'm not sure whats wrong.
Author
Owner

@stephanebruckert commented on GitHub (Dec 31, 2022):

Would you have a complete stack trace?

<!-- gh-comment-id:1368200962 --> @stephanebruckert commented on GitHub (Dec 31, 2022): Would you have a complete stack trace?
Author
Owner

@Andrew3212 commented on GitHub (Dec 31, 2022):

I went back and tested it and whenever this third line is called it gives the error

cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing', cache_handler=cache_handler)    
if not auth_manager.validate_token(cache_handler.get_cached_token()):
Traceback (most recent call last):
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\app.py", line 1038, in handle_message_create_for_prefix_commands
    await self.process_prefix_commands(context)
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\app.py", line 1010, in process_prefix_commands
    await context.invoke()
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\context\base.py", line 311, in invoke
    await self.command.invoke(self)
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\commands\prefix.py", line 119, in invoke
    await self(context, **kwargs)
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\commands\base.py", line 605, in __call__
    return await self.callback(context, **kwargs)
  File "C:\Users\arg\OneDrive\Documents\DiscordBotProject\bot.py", line 142, in currently_playing
    if not auth_manager.validate_token(cache_handler.get_cached_token()):
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\spotipy\cache_handler.py", line 163, in get_cached_token
    token_info = self.session["token_info"]
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\werkzeug\local.py", line 316, in __get__
    obj = instance._get_current_object()  # type: ignore[misc]
  File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\werkzeug\local.py", line 513, in _get_current_object
    raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of request context.
<!-- gh-comment-id:1368201453 --> @Andrew3212 commented on GitHub (Dec 31, 2022): I went back and tested it and whenever this third line is called it gives the error ``` cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session) auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing', cache_handler=cache_handler) if not auth_manager.validate_token(cache_handler.get_cached_token()): ``` ``` Traceback (most recent call last): File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\app.py", line 1038, in handle_message_create_for_prefix_commands await self.process_prefix_commands(context) File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\app.py", line 1010, in process_prefix_commands await context.invoke() File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\context\base.py", line 311, in invoke await self.command.invoke(self) File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\commands\prefix.py", line 119, in invoke await self(context, **kwargs) File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\lightbulb\commands\base.py", line 605, in __call__ return await self.callback(context, **kwargs) File "C:\Users\arg\OneDrive\Documents\DiscordBotProject\bot.py", line 142, in currently_playing if not auth_manager.validate_token(cache_handler.get_cached_token()): File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\spotipy\cache_handler.py", line 163, in get_cached_token token_info = self.session["token_info"] File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\werkzeug\local.py", line 316, in __get__ obj = instance._get_current_object() # type: ignore[misc] File "C:\Users\arg\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\werkzeug\local.py", line 513, in _get_current_object raise RuntimeError(unbound_message) from None RuntimeError: Working outside of request context. ```
Author
Owner

@stephanebruckert commented on GitHub (Dec 31, 2022):

Okay I'm not sure to be honest.

Have you tried running the example as it is in https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py and does it work? I've just tried it again and it works for me.

I don't know your full code, what steps you took and whether the lines you added have an impact:

@bot.command
@lightbulb.command('c', 'Current Song')
@lightbulb.implements(lightbulb.PrefixCommand)

Have you checked if any of the answers here help? https://stackoverflow.com/q/9931738/1515819

<!-- gh-comment-id:1368224192 --> @stephanebruckert commented on GitHub (Dec 31, 2022): Okay I'm not sure to be honest. Have you tried running the example as it is in https://github.com/spotipy-dev/spotipy/blob/master/examples/app.py and does it work? I've just tried it again and it works for me. I don't know your full code, what steps you took and whether the lines you added have an impact: @bot.command @lightbulb.command('c', 'Current Song') @lightbulb.implements(lightbulb.PrefixCommand) Have you checked if any of the answers here help? https://stackoverflow.com/q/9931738/1515819
Author
Owner

@Andrew3212 commented on GitHub (Dec 31, 2022):

I figured it out. Had to run flask on a different thread and had to switch ports as well.
Sorry about that, and thanks.

def flaskfrog():
    if __name__ == '__name__':
        app.run(5000, use_reloader=False)
    app.run(port=5000, use_reloader=False)

t_flask = threading.Thread(target=flaskfrog)
t_flask.start()
<!-- gh-comment-id:1368286819 --> @Andrew3212 commented on GitHub (Dec 31, 2022): I figured it out. Had to run flask on a different thread and had to switch ports as well. Sorry about that, and thanks. ``` def flaskfrog(): if __name__ == '__name__': app.run(5000, use_reloader=False) app.run(port=5000, use_reloader=False) t_flask = threading.Thread(target=flaskfrog) t_flask.start() ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#553
No description provided.