[GH-ISSUE #21] ValueError: Substring "accessToken":" not found in JSON string #15

Closed
opened 2026-02-27 19:06:26 +03:00 by kerem · 7 comments
Owner

Originally created by @invzfnc on GitHub (Mar 10, 2025).
Original GitHub issue: https://github.com/Aran404/SpotAPI/issues/21

Version

  • Python 3.11.4
  • spotapi==1.1.4

Code to reproduce the error

from spotapi import Public

result = next(Public.playlist_info("16tsE24qp7IeWHxARnF8Zs"))

Result: yields ValueError

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    result = next(Public.playlist_info("16tsE24qp7IeWHxARnF8Zs"))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\public.py", line 82, in playlist_info
    yield from playlist.paginate_playlist()
  File "venv\Lib\site-packages\spotapi\playlist.py", line 93, in paginate_playlist
    playlist = self.get_playlist_info(limit=UPPER_LIMIT)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper
    result: R = func(*args, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\playlist.py", line 69, in get_playlist_info
    "sha256Hash": self.base.part_hash("fetchPlaylist"),
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper
    result: R = func(*args, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\client.py", line 134, in part_hash
    self.get_sha256_hash()
  File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper
    result: R = func(*args, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\client.py", line 146, in get_sha256_hash
    self.get_session()
  File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper
    result: R = func(*args, **kwargs)
                ^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\client.py", line 85, in get_session
    self.access_token = parse_json_string(resp.response, "accessToken")
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv\Lib\site-packages\spotapi\utils\strings.py", line 43, in parse_json_string
    raise ValueError(f'Substring "{s}":" not found in JSON string')
ValueError: Substring "accessToken":" not found in JSON string

Expected behavior
playlist_info should return playlist details without errors.

Originally created by @invzfnc on GitHub (Mar 10, 2025). Original GitHub issue: https://github.com/Aran404/SpotAPI/issues/21 **Version** - Python 3.11.4 - `spotapi==1.1.4` **Code to reproduce the error** ```python from spotapi import Public result = next(Public.playlist_info("16tsE24qp7IeWHxARnF8Zs")) ``` **Result: yields `ValueError`** ``` Traceback (most recent call last): File "test.py", line 3, in <module> result = next(Public.playlist_info("16tsE24qp7IeWHxARnF8Zs")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\public.py", line 82, in playlist_info yield from playlist.paginate_playlist() File "venv\Lib\site-packages\spotapi\playlist.py", line 93, in paginate_playlist playlist = self.get_playlist_info(limit=UPPER_LIMIT) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper result: R = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\playlist.py", line 69, in get_playlist_info "sha256Hash": self.base.part_hash("fetchPlaylist"), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper result: R = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\client.py", line 134, in part_hash self.get_sha256_hash() File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper result: R = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\client.py", line 146, in get_sha256_hash self.get_session() File "venv\Lib\site-packages\spotapi\types\annotations.py", line 47, in wrapper result: R = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\client.py", line 85, in get_session self.access_token = parse_json_string(resp.response, "accessToken") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "venv\Lib\site-packages\spotapi\utils\strings.py", line 43, in parse_json_string raise ValueError(f'Substring "{s}":" not found in JSON string') ValueError: Substring "accessToken":" not found in JSON string ``` **Expected behavior** `playlist_info` should return playlist details without errors.
kerem closed this issue 2026-02-27 19:06:27 +03:00
Author
Owner

@TheNooB2706 commented on GitHub (Mar 11, 2025):

It seems that Spotify had changed on their side that the access token is no longer included in the initial response, but has to be requested separately from https://open.spotify.com/get_access_token. The URL seems accessible without any specific request header that you can visit it directly on a browser, in that case maybe the initial request to https://open.spotify.com can be dropped entirely replaced with this in the get_session method, but I am not very sure.

github.com/Aran404/SpotAPI@059064565d/spotapi/client.py (L66-L86)

<!-- gh-comment-id:2712756234 --> @TheNooB2706 commented on GitHub (Mar 11, 2025): It seems that Spotify had changed on their side that the access token is no longer included in the initial response, but has to be requested separately from `https://open.spotify.com/get_access_token`. The URL seems accessible without any specific request header that you can visit it directly on a browser, in that case maybe the initial request to `https://open.spotify.com` can be dropped entirely replaced with this in the [get_session](https://github.com/Aran404/SpotAPI/blob/059064565dfa16901c03b5dc19d21ff67c4ba220/spotapi/client.py#L66) method, but I am not very sure. https://github.com/Aran404/SpotAPI/blob/059064565dfa16901c03b5dc19d21ff67c4ba220/spotapi/client.py#L66-L86
Author
Owner

@invzfnc commented on GitHub (Mar 11, 2025):

You're right about this. I tried sending a simple request to https://open.spotify.com/get_access_token and have parse_json_string parse the response text instead.

import requests
res = requests.get("https://open.spotify.com/get_access_token")

self.access_token = parse_json_string(res.text, "accessToken")
self.client_id = parse_json_string(res.text, "clientId")
self.device_id = parse_json_string(resp.response, "correlationId")

playlist_info is working with this change. But I'm unsure about the compatibility with other functions in the library, I might need the owner's opinion on this, also should I open a PR for this?

<!-- gh-comment-id:2713994182 --> @invzfnc commented on GitHub (Mar 11, 2025): You're right about this. I tried sending a simple request to `https://open.spotify.com/get_access_token` and have `parse_json_string` parse the response text instead. ```python import requests res = requests.get("https://open.spotify.com/get_access_token") self.access_token = parse_json_string(res.text, "accessToken") self.client_id = parse_json_string(res.text, "clientId") self.device_id = parse_json_string(resp.response, "correlationId") ``` `playlist_info` is working with this change. But I'm unsure about the compatibility with other functions in the library, I might need the owner's opinion on this, also should I open a PR for this?
Author
Owner

@TheNooB2706 commented on GitHub (Mar 11, 2025):

Maybe you can open a PR first and see what the owner have to say. I wasn't actually using this library, just searching around to see if there's any solution available because my own code is facing the same problem and stumbled upon this.

If really want to follow what the Spotify web app is doing, the request to https://open.spotify.com/get_access_token is accompanied with URL parameters of reason=init and productType=web-player and request header Cookie of sp_t=<correlationId>; sp_landing=https%3A%2F%2Fopen.spotify.com%2F%3Fsp_cid%3D<correlationId>%26device%3Ddesktop, assuming these are not automatically added to every request. At least this is how it behave on my system. And I assume the other things are not important.

Image

<!-- gh-comment-id:2714571283 --> @TheNooB2706 commented on GitHub (Mar 11, 2025): Maybe you can open a PR first and see what the owner have to say. I wasn't actually using this library, just searching around to see if there's any solution available because my own code is facing the same problem and stumbled upon this. If really want to follow what the Spotify web app is doing, the request to `https://open.spotify.com/get_access_token` is accompanied with URL parameters of `reason=init` and `productType=web-player` and request header `Cookie` of `sp_t=<correlationId>; sp_landing=https%3A%2F%2Fopen.spotify.com%2F%3Fsp_cid%3D<correlationId>%26device%3Ddesktop`, assuming these are not automatically added to every request. At least this is how it behave on my system. And I assume the other things are not important. ![Image](https://github.com/user-attachments/assets/59c2c382-ff26-4f0d-bce1-d79e2a33d6bf)
Author
Owner

@Aran404 commented on GitHub (Mar 11, 2025):

Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface.

<!-- gh-comment-id:2714862319 --> @Aran404 commented on GitHub (Mar 11, 2025): Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface.
Author
Owner

@afkarxyz commented on GitHub (Mar 11, 2025):

Maybe you can open a PR first and see what the owner have to say. I wasn't actually using this library, just searching around to see if there's any solution available because my own code is facing the same problem and stumbled upon this.

If really want to follow what the Spotify web app is doing, the request to https://open.spotify.com/get_access_token is accompanied with URL parameters of reason=init and productType=web-player and request header Cookie of sp_t=<correlationId>; sp_landing=https%3A%2F%2Fopen.spotify.com%2F%3Fsp_cid%3D<correlationId>%26device%3Ddesktop, assuming these are not automatically added to every request. At least this is how it behave on my system. And I assume the other things are not important.

Image

What browser are you using?

<!-- gh-comment-id:2715706993 --> @afkarxyz commented on GitHub (Mar 11, 2025): > Maybe you can open a PR first and see what the owner have to say. I wasn't actually using this library, just searching around to see if there's any solution available because my own code is facing the same problem and stumbled upon this. > > If really want to follow what the Spotify web app is doing, the request to `https://open.spotify.com/get_access_token` is accompanied with URL parameters of `reason=init` and `productType=web-player` and request header `Cookie` of `sp_t=<correlationId>; sp_landing=https%3A%2F%2Fopen.spotify.com%2F%3Fsp_cid%3D<correlationId>%26device%3Ddesktop`, assuming these are not automatically added to every request. At least this is how it behave on my system. And I assume the other things are not important. > > ![Image](https://github.com/user-attachments/assets/59c2c382-ff26-4f0d-bce1-d79e2a33d6bf) What browser are you using?
Author
Owner

@scottsisco commented on GitHub (Mar 12, 2025):

Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface.

I just pulled the newest version of SpotAPI using pip and I can confirm the issue has been fixed for me.

<!-- gh-comment-id:2716020997 --> @scottsisco commented on GitHub (Mar 12, 2025): > Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface. I just pulled the newest version of SpotAPI using pip and I can confirm the issue has been fixed for me.
Author
Owner

@invzfnc commented on GitHub (Mar 12, 2025):

Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface.

It works perfectly. Thank you! I'm closing the issue now.

<!-- gh-comment-id:2716170660 --> @invzfnc commented on GitHub (Mar 12, 2025): > Should be fixed now, let me know if it works properly. Thank you for bringing issue to the surface. It works perfectly. Thank you! I'm closing the issue now.
Sign in to join this conversation.
No labels
pull-request
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/SpotAPI#15
No description provided.