[GH-ISSUE #222] [BUG] Credentials loading crashes with a json parse error #37

Closed
opened 2026-02-27 08:11:33 +03:00 by kerem · 5 comments
Owner

Originally created by @rathmerdominik on GitHub (Aug 29, 2023).
Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/222

Describe the bug
Credentials checking or loading can crash with an error when Spotify has a Server error.

To Reproduce
Steps to reproduce the behavior:

  1. Be unlucky
  2. Try to use librespot.core.Session.Builder().user_pass(username, password).create()
  3. See
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/requests/models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/__init__.py", line 514, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/decoder.py", line 386, in decode
    obj, end = self.raw_decode(s)
               ^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/decoder.py", line 416, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
simplejson.errors.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dominik/Projects/klang-spiegel/src/klang_spiegel/app.py", line 336, in <module>
    session = setup_spotify()
              ^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/src/klang_spiegel/app.py", line 284, in setup_spotify
    return Session.Builder().user_pass(username, password).create()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 1365, in create
    ApResolver.get_random_accesspoint(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 305, in get_random_accesspoint
    return ApResolver.get_random_of("accesspoint")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 274, in get_random_of
    pool = ApResolver.request(service_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 263, in request
    return response.json()
           ^^^^^^^^^^^^^^^
  File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 2 column 1 (char 1)
  1. I have printed the result at that point that gets sent back from Spotify. This is the answer:
b'\n<html><head>\n<meta http-equiv="content-type" content="text/html;charset=utf-8">\n<title>502 Server Error</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: Server Error</h1>\n<h2>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds.</h2>\n<h2></h2>\n</body></html>\n'

Expected behavior
Credentials are read out and a Session is established

Client Information (please complete the following information):

  • OS: archlinux
  • Python Version 3.11.3
  • Library Version 0.0.9
Originally created by @rathmerdominik on GitHub (Aug 29, 2023). Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/222 **Describe the bug** Credentials checking or loading can crash with an error when Spotify has a Server error. **To Reproduce** Steps to reproduce the behavior: 1. Be unlucky 2. Try to use `librespot.core.Session.Builder().user_pass(username, password).create()` 3. See ``` File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/requests/models.py", line 971, in json return complexjson.loads(self.text, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/__init__.py", line 514, in loads return _default_decoder.decode(s) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/decoder.py", line 386, in decode obj, end = self.raw_decode(s) ^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/simplejson/decoder.py", line 416, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ simplejson.errors.JSONDecodeError: Expecting value: line 2 column 1 (char 1) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/dominik/Projects/klang-spiegel/src/klang_spiegel/app.py", line 336, in <module> session = setup_spotify() ^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/src/klang_spiegel/app.py", line 284, in setup_spotify return Session.Builder().user_pass(username, password).create() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 1365, in create ApResolver.get_random_accesspoint(), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 305, in get_random_accesspoint return ApResolver.get_random_of("accesspoint") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 274, in get_random_of pool = ApResolver.request(service_type) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/librespot/core.py", line 263, in request return response.json() ^^^^^^^^^^^^^^^ File "/home/dominik/Projects/klang-spiegel/venv/lib/python3.11/site-packages/requests/models.py", line 975, in json raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 2 column 1 (char 1) ``` 5. I have printed the result at that point that gets sent back from Spotify. This is the answer: ``` b'\n<html><head>\n<meta http-equiv="content-type" content="text/html;charset=utf-8">\n<title>502 Server Error</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: Server Error</h1>\n<h2>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds.</h2>\n<h2></h2>\n</body></html>\n' ``` **Expected behavior** Credentials are read out and a Session is established **Client Information (please complete the following information):** - OS: archlinux - Python Version 3.11.3 - Library Version 0.0.9
kerem 2026-02-27 08:11:33 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@kokarare1212 commented on GitHub (Aug 29, 2023):

So an API that returns an access point returns html if it is rate-limited...
Is it appropriate to use the raise_for_status function of the request?

<!-- gh-comment-id:1697508736 --> @kokarare1212 commented on GitHub (Aug 29, 2023): So an API that returns an access point returns html if it is rate-limited... Is it appropriate to use the raise_for_status function of the request?
Author
Owner

@rathmerdominik commented on GitHub (Aug 29, 2023):

So an API that returns an access point returns html if it is rate-limited... Is it appropriate to use the raise_for_status function of the request?

Yeah. They really had a great idea there....

I am not sure what can be done if this occurs.
I doubt it is possible to circumvent this. So maybe a nice error saying "You are being rate limited by spotify, or another issue is occuring" would help.

Maybe it is possible to work around it.

It also does not seem to be an IP rate limit (If it is a rate limit at all. The error states it is a "server error") as switching IP with Mullvad or connecting to my home network via sshuttle does not do anything to resolve the situation

<!-- gh-comment-id:1697527961 --> @rathmerdominik commented on GitHub (Aug 29, 2023): > So an API that returns an access point returns html if it is rate-limited... Is it appropriate to use the raise_for_status function of the request? Yeah. They really had a great idea there.... I am not sure what can be done if this occurs. I doubt it is possible to circumvent this. So maybe a nice error saying "You are being rate limited by spotify, or another issue is occuring" would help. Maybe it is possible to work around it. It also does not seem to be an IP rate limit (If it is a rate limit at all. The error states it is a "server error") as switching IP with Mullvad or connecting to my home network via sshuttle does not do anything to resolve the situation
Author
Owner

@rathmerdominik commented on GitHub (Aug 29, 2023):

image
Also this is the spot where i entered the print. If it helps at all

<!-- gh-comment-id:1697535227 --> @rathmerdominik commented on GitHub (Aug 29, 2023): ![image](https://github.com/kokarare1212/librespot-python/assets/13904508/c584a034-8377-4c48-a45b-1e11155159f9) Also this is the spot where i entered the print. If it helps at all
Author
Owner

@rathmerdominik commented on GitHub (Aug 29, 2023):

image
There really is a server error right now apparently.
Crazy

<!-- gh-comment-id:1697537224 --> @rathmerdominik commented on GitHub (Aug 29, 2023): ![image](https://github.com/kokarare1212/librespot-python/assets/13904508/f9f841fc-f098-41e2-845c-de36616e4557) There really is a server error right now apparently. Crazy
Author
Owner

@rathmerdominik commented on GitHub (Aug 29, 2023):

This was really a server issue that has now been resolved.
So I changed the issue description.

This is still an edge case that needs handling.
But it is now certain that you can't do much if this occurs

<!-- gh-comment-id:1697582859 --> @rathmerdominik commented on GitHub (Aug 29, 2023): This was really a server issue that has now been resolved. So I changed the issue description. This is still an edge case that needs handling. But it is now certain that you can't do much if this occurs
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/librespot-python-kokarare1212#37
No description provided.