[GH-ISSUE #1025] JSONDecodeError when trying to use auth code flow on a headless device #610

Closed
opened 2026-02-28 00:00:15 +03:00 by kerem · 1 comment
Owner

Originally created by @pvilimas on GitHub (Aug 31, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1025

I'm doing a project on my headless raspberry pi over SSH. When I run this script on my main PC, it outputs as expected my most recently added songs. When I run it on the pi, it always gives a JSONDecodeError (full error below). I've tried doing this a couple different ways but like I said my device is headless so I need to somehow authenticate on startup and without opening a browser window.

Script:

#!/usr/bin/python3

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import os

CLIENT_ID = '...'
CLIENT_SECRET = '...'
REDIRECT_URI = 'http://localhost:8888/callback'

os.environ['SPOTIPY_CLIENT_ID'] = CLIENT_ID
os.environ['SPOTIPY_CLIENT_SECRET'] = CLIENT_SECRET
os.environ['SPOTIPY_REDIRECT_URI'] = REDIRECT_URI

scope = "user-library-read"

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    redirect_uri=REDIRECT_URI,
    scope=scope,
    open_browser=False))

results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
    track = item['track']
    print(idx, track['artists'][0]['name'], " – ", track['name'])

Error:

Traceback (most recent call last):
  File "./spotipy_test", line 34, in <module>
    results = sp.current_user_saved_tracks()
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 1232, in current_user_saved_tracks
    return self._get("me/tracks", limit=limit, offset=offset, market=market)
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 221, in _internal_call
    headers = self._auth_headers()
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 212, in _auth_headers
    token = self.auth_manager.get_access_token(as_dict=False)
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 525, in get_access_token
    token_info = self.validate_token(self.cache_handler.get_cached_token())
  File "/home/pi/.local/lib/python3.7/site-packages/spotipy/cache_handler.py", line 70, in get_cached_token
    token_info = json.loads(token_info_string)
  File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Originally created by @pvilimas on GitHub (Aug 31, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1025 I'm doing a project on my headless raspberry pi over SSH. When I run this script on my main PC, it outputs as expected my most recently added songs. When I run it on the pi, it always gives a JSONDecodeError (full error below). I've tried doing this a couple different ways but like I said my device is headless so I need to somehow authenticate on startup and without opening a browser window. Script: ``` #!/usr/bin/python3 import spotipy from spotipy.oauth2 import SpotifyOAuth import os CLIENT_ID = '...' CLIENT_SECRET = '...' REDIRECT_URI = 'http://localhost:8888/callback' os.environ['SPOTIPY_CLIENT_ID'] = CLIENT_ID os.environ['SPOTIPY_CLIENT_SECRET'] = CLIENT_SECRET os.environ['SPOTIPY_REDIRECT_URI'] = REDIRECT_URI scope = "user-library-read" sp = spotipy.Spotify(auth_manager=SpotifyOAuth( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI, scope=scope, open_browser=False)) results = sp.current_user_saved_tracks() for idx, item in enumerate(results['items']): track = item['track'] print(idx, track['artists'][0]['name'], " – ", track['name']) ``` Error: ``` Traceback (most recent call last): File "./spotipy_test", line 34, in <module> results = sp.current_user_saved_tracks() File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 1232, in current_user_saved_tracks return self._get("me/tracks", limit=limit, offset=offset, market=market) File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 297, in _get return self._internal_call("GET", url, payload, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 221, in _internal_call headers = self._auth_headers() File "/home/pi/.local/lib/python3.7/site-packages/spotipy/client.py", line 212, in _auth_headers token = self.auth_manager.get_access_token(as_dict=False) File "/home/pi/.local/lib/python3.7/site-packages/spotipy/oauth2.py", line 525, in get_access_token token_info = self.validate_token(self.cache_handler.get_cached_token()) File "/home/pi/.local/lib/python3.7/site-packages/spotipy/cache_handler.py", line 70, in get_cached_token token_info = json.loads(token_info_string) File "/usr/lib/python3.7/json/__init__.py", line 348, in loads return _default_decoder.decode(s) File "/usr/lib/python3.7/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ```
kerem 2026-02-28 00:00:15 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@hansmbakker commented on GitHub (Sep 9, 2023):

There is a request for allowing the Device Authorization Grant flow on the Spotify forum - it is very useful for embedded usecases like a remote control.

<!-- gh-comment-id:1712514410 --> @hansmbakker commented on GitHub (Sep 9, 2023): There is a [request for allowing the Device Authorization Grant flow](https://community.spotify.com/t5/Spotify-for-Developers/Device-Authorization-Grant-authentication-flow-for-custom/m-p/5485468) on the Spotify forum - it is very useful for embedded usecases like a remote control.
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#610
No description provided.