From 7497469d5ed7ad1e407c501d673c6bf71ed4a5c8 Mon Sep 17 00:00:00 2001 From: Developer Date: Mon, 6 Jul 2026 15:45:51 +0000 Subject: [PATCH] Fix: user_id 'admin' rejected by Jellyfin API (400) The Jellyfin service config's user_id field accepts either the internal Jellyfin user ID (a long hash) or a username (e.g. 'admin'). The worker passed the raw value directly to client.libraries(user_id), but Jellyfin's API rejects usernames with a 400. Now validates the configured user_id against the Jellyfin users API: 1. If it matches a user's Id (internal hash), use it directly. 2. If it matches a user's Name (username like 'admin'), resolve the Id. 3. If no match, fall back to the first user and log a warning. 283 backend tests pass; ruff clean. --- .../workers/media_index_worker.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend/src/media_library_viewer_api/workers/media_index_worker.py b/backend/src/media_library_viewer_api/workers/media_index_worker.py index 82411c5..530191a 100644 --- a/backend/src/media_library_viewer_api/workers/media_index_worker.py +++ b/backend/src/media_library_viewer_api/workers/media_index_worker.py @@ -113,6 +113,29 @@ def _resolve_jellyfin(service_id: str) -> tuple[Any, str]: if not users: raise RuntimeError("No Jellyfin users found and no user_id configured on the service") user_id = users[0]["Id"] + else: + # The config field accepts either a Jellyfin internal user ID (a long + # hash) or a username (e.g. "admin"). Validate against the users API: + # if the configured value doesn't match any user's Id, try matching by + # Name, then fall back to the first user. + users = client.users() + valid_ids = {str(u.get("Id", "")) for u in users} + if user_id not in valid_ids: + match = next((u for u in users if str(u.get("Name", "")) == user_id), None) + if match: + user_id = match["Id"] + logger.info( + "Resolved username '%s' to Jellyfin Id '%s'", + service.get("config", {}).get("user_id"), + user_id, + ) + elif users: + user_id = users[0]["Id"] + logger.warning( + "user_id '%s' not found; falling back to first user '%s'", + service.get("config", {}).get("user_id"), + user_id, + ) return client, user_id