spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

JSONDecodeError when trying to use auth code flow on a headless device #1025

Closed pvilimas closed 1 year ago

pvilimas commented 1 year ago

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)
hansmbakker commented 1 year ago

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.