[GH-ISSUE #1198] Calls with local server is frozen #702

Closed
opened 2026-02-28 00:00:59 +03:00 by kerem · 9 comments
Owner

Originally created by @GuyKh on GitHub (May 14, 2025).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1198

Describe the bug
A clear and concise description of what the bug is.
Some context - I'm running raspotify service and try to trigger playing a track through spotipy.
Have redirect url configured in Spotipy settings.
Also - up until changes in their API - it used to work with redirect url = http://192.168.1.100:8080

Your code

REDIRECT_URL = "http://127.0.0.1:8080"

    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URL, requests_timeout=5, scope=scope, username=USERNAME))
    print("sp initialized")
    print(sp.devices())

Expected behavior
return a list of devices.

Output
Logs are stuck after sp initialized.
cmd+c gives this stack trace:

python3 test.py 
sp initialized
^C Traceback (most recent call last):
  File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module>
    print(sp.devices())
          ^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices
    return self._get("me/player/devices")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 250, in _internal_call
    headers = self._auth_headers()
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 241, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 511, in get_access_token
    "code": code or self.get_auth_response(),
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 467, in get_auth_response
    return self._get_auth_response_local_server(redirect_port)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 425, in _get_auth_response_local_server
    server.handle_request()
  File "/usr/lib/python3.11/socketserver.py", line 295, in handle_request
    ready = selector.select(timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)

Environment:

  • OS: Linus
  • Python version 3.12
  • spotipy version 2.25.1

Additional context
This is unrelated to passed scopes etc.

Originally created by @GuyKh on GitHub (May 14, 2025). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1198 **Describe the bug** A clear and concise description of what the bug is. Some context - I'm running **raspotify** service and try to trigger playing a track through `spotipy`. Have redirect url configured in Spotipy settings. Also - up until changes in their API - it used to work with redirect url = `http://192.168.1.100:8080` **Your code** ``` REDIRECT_URL = "http://127.0.0.1:8080" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URL, requests_timeout=5, scope=scope, username=USERNAME)) print("sp initialized") print(sp.devices()) ``` **Expected behavior** return a list of devices. **Output** Logs are stuck after `sp initialized`. `cmd+c` gives this stack trace: ``` python3 test.py sp initialized ^C Traceback (most recent call last): File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module> print(sp.devices()) ^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices return self._get("me/player/devices") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 250, in _internal_call headers = self._auth_headers() ^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 241, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 511, in get_access_token "code": code or self.get_auth_response(), ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 467, in get_auth_response return self._get_auth_response_local_server(redirect_port) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 425, in _get_auth_response_local_server server.handle_request() File "/usr/lib/python3.11/socketserver.py", line 295, in handle_request ready = selector.select(timeout) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/selectors.py", line 415, in select fd_event_list = self._selector.poll(timeout) ``` **Environment:** - OS: Linus - Python version 3.12 - spotipy version 2.25.1 **Additional context** This is unrelated to passed scopes etc.
kerem 2026-02-28 00:00:59 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@GuyKh commented on GitHub (May 14, 2025):

An additional observation;
When running nmap 127.0.0.1 -p 8000-9000 while in this stuck state.
The app crashes with:

(env) pi@raspberrypi:~/Spotify-RFID-Record-Player $ python3 test.py 
sp initialized
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 58170)
Traceback (most recent call last):
  File "/usr/lib/python3.11/socketserver.py", line 317, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.11/socketserver.py", line 348, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.11/socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.11/socketserver.py", line 755, in __init__
    self.handle()
  File "/usr/lib/python3.11/http/server.py", line 432, in handle
    self.handle_one_request()
  File "/usr/lib/python3.11/http/server.py", line 400, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/socket.py", line 706, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
ConnectionResetError: [Errno 104] Connection reset by peer
----------------------------------------
Traceback (most recent call last):
  File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module>
    print(sp.devices())
          ^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices
    return self._get("me/player/devices")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 250, in _internal_call
    headers = self._auth_headers()
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 241, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 511, in get_access_token
    "code": code or self.get_auth_response(),
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 467, in get_auth_response
    return self._get_auth_response_local_server(redirect_port)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 434, in _get_auth_response_local_server
    raise SpotifyOauthError("Server listening on localhost has not been accessed")
spotipy.exceptions.SpotifyOauthError: Server listening on localhost has not been accessed
<!-- gh-comment-id:2879076663 --> @GuyKh commented on GitHub (May 14, 2025): An additional observation; When running `nmap 127.0.0.1 -p 8000-9000` while in this stuck state. The app crashes with: ``` (env) pi@raspberrypi:~/Spotify-RFID-Record-Player $ python3 test.py sp initialized ---------------------------------------- Exception occurred during processing of request from ('127.0.0.1', 58170) Traceback (most recent call last): File "/usr/lib/python3.11/socketserver.py", line 317, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python3.11/socketserver.py", line 348, in process_request self.finish_request(request, client_address) File "/usr/lib/python3.11/socketserver.py", line 361, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.11/socketserver.py", line 755, in __init__ self.handle() File "/usr/lib/python3.11/http/server.py", line 432, in handle self.handle_one_request() File "/usr/lib/python3.11/http/server.py", line 400, in handle_one_request self.raw_requestline = self.rfile.readline(65537) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/socket.py", line 706, in readinto return self._sock.recv_into(b) ^^^^^^^^^^^^^^^^^^^^^^^ ConnectionResetError: [Errno 104] Connection reset by peer ---------------------------------------- Traceback (most recent call last): File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module> print(sp.devices()) ^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices return self._get("me/player/devices") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 250, in _internal_call headers = self._auth_headers() ^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 241, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 511, in get_access_token "code": code or self.get_auth_response(), ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 467, in get_auth_response return self._get_auth_response_local_server(redirect_port) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/oauth2.py", line 434, in _get_auth_response_local_server raise SpotifyOauthError("Server listening on localhost has not been accessed") spotipy.exceptions.SpotifyOauthError: Server listening on localhost has not been accessed ```
Author
Owner

@GuyKh commented on GitHub (May 14, 2025):

As this is a server-to-server flow, I've also considered using SpotifyClientCredentials

Code:

    client_credentials_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
    sp = spotipy.Spotify(auth_manager=client_credentials_manager)
    print("sp initialized")
    print(sp.devices())

Output:

sp initialized
HTTP Error for GET to https://api.spotify.com/v1/me/player/devices with Params: {} returned 404 due to Invalid username
Traceback (most recent call last):
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 274, in _internal_call
    response.raise_for_status()
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/devices

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module>
    print(sp.devices())
          ^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices
    return self._get("me/player/devices")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 294, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 404, code: -1 - https://api.spotify.com/v1/me/player/devices:
 Invalid username, reason: None
<!-- gh-comment-id:2879084236 --> @GuyKh commented on GitHub (May 14, 2025): As this is a server-to-server flow, I've also considered using `SpotifyClientCredentials` **Code:** ``` client_credentials_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) sp = spotipy.Spotify(auth_manager=client_credentials_manager) print("sp initialized") print(sp.devices()) ``` **Output:** ``` sp initialized HTTP Error for GET to https://api.spotify.com/v1/me/player/devices with Params: {} returned 404 due to Invalid username Traceback (most recent call last): File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 274, in _internal_call response.raise_for_status() File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/requests/models.py", line 1024, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/me/player/devices During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/pi/Spotify-RFID-Record-Player/test.py", line 34, in <module> print(sp.devices()) ^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 1813, in devices return self._get("me/player/devices") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 324, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/pi/Spotify-RFID-Record-Player/env/lib/python3.11/site-packages/spotipy/client.py", line 294, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 404, code: -1 - https://api.spotify.com/v1/me/player/devices: Invalid username, reason: None ```
Author
Owner

@dieser-niko commented on GitHub (May 14, 2025):

I'll start with the SpotifyClientCredentials, since it's easier to explain why it's not working.
When you use that auth flow, your app doesn't bind to a user, meaning you won't have access to a player.

Regarding the other issue, it looks like the server is waiting for a callback. Could you try to pass open_browser=False to SpotifyOAuth as shown in headless.py? You should then get a link where you can log in with your account.

Alternatively you could share your authorization flow.

<!-- gh-comment-id:2879475068 --> @dieser-niko commented on GitHub (May 14, 2025): I'll start with the `SpotifyClientCredentials`, since it's easier to explain why it's not working. When you use that auth flow, your app doesn't bind to a user, meaning you won't have access to a player. Regarding the other issue, it looks like the server is waiting for a callback. Could you try to pass `open_browser=False` to `SpotifyOAuth` as shown in [headless.py](https://github.com/spotipy-dev/spotipy-examples/blob/c610a79705ef4aa55e4d61572a012f77b6f7245d/scripts/headless.py)? You should then get a link where you can log in with your account. Alternatively you could share your authorization flow.
Author
Owner

@GuyKh commented on GitHub (May 14, 2025):

@dieser-niko the purpose of this is a service to run (headless indeed) - and send Play media X to a certain device.
Is the open_browser=false relevant in this case?

<!-- gh-comment-id:2879926187 --> @GuyKh commented on GitHub (May 14, 2025): @dieser-niko the purpose of this is a service to run (headless indeed) - and send `Play media X` to a certain device. Is the `open_browser=false` relevant in this case?
Author
Owner

@dieser-niko commented on GitHub (May 14, 2025):

Yes, spotipy opens the browser with the authentication url. If there's no browser or you only have a CLI, this step will fail.

If you set open_browser to False, then the auth url will be printed instead.

<!-- gh-comment-id:2879934756 --> @dieser-niko commented on GitHub (May 14, 2025): Yes, spotipy opens the browser with the authentication url. If there's no browser or you only have a CLI, this step will fail. If you set `open_browser` to `False`, then the auth url will be printed instead.
Author
Owner

@dieser-niko commented on GitHub (May 14, 2025):

Alternatively you could implement your own flow, another example would be an implementation with FastAPI:
https://github.com/dieser-niko/spotipy-fastapi-oauth

But I think the headless example might be the best option for you.

<!-- gh-comment-id:2879951428 --> @dieser-niko commented on GitHub (May 14, 2025): Alternatively you could implement your own flow, another example would be an implementation with FastAPI: https://github.com/dieser-niko/spotipy-fastapi-oauth But I think the headless example might be the best option for you.
Author
Owner

@GuyKh commented on GitHub (May 14, 2025):

@dieser-niko any idea about token refresh? The intention is for it to run on a raspberrypi with RF card reader where every card is a different song.

The thing is that there could be a long time (days) between card swipes. So I cant have a re-login flow once in a while.

Any idea about that?

<!-- gh-comment-id:2880804058 --> @GuyKh commented on GitHub (May 14, 2025): @dieser-niko any idea about token refresh? The intention is for it to run on a raspberrypi with RF card reader where every card is a different song. The thing is that there could be a long time (days) between card swipes. So I cant have a re-login flow once in a while. Any idea about that?
Author
Owner

@dieser-niko commented on GitHub (May 14, 2025):

Usually, you don't need to re-authenticate that often, if at all. But to be honest, I have no idea how long a refresh token lasts. The only information I could find on it is here: https://github.com/spotify/web-api/issues/911#issuecomment-581104335

<!-- gh-comment-id:2880856695 --> @dieser-niko commented on GitHub (May 14, 2025): Usually, you don't need to re-authenticate that often, if at all. But to be honest, I have no idea how long a refresh token lasts. The only information I could find on it is here: https://github.com/spotify/web-api/issues/911#issuecomment-581104335
Author
Owner

@GuyKh commented on GitHub (May 14, 2025):

Looks like this works flawlessly with open_browser=False
Thanks

<!-- gh-comment-id:2880928322 --> @GuyKh commented on GitHub (May 14, 2025): Looks like this works flawlessly with `open_browser=False` Thanks
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#702
No description provided.