#!/usr/bin/env python3 """ Debug script to test API authentication """ import os import requests from dotenv import load_dotenv load_dotenv() def test_service_api(service_name, base_url, api_path, api_key_header='X-Api-Key'): """Test API authentication for a service""" api_key = os.getenv(f'{service_name.upper()}_API_KEY') if not api_key: print(f"❌ {service_name}: No API key found") return url = f"{base_url}{api_path}" headers = {api_key_header: api_key} try: response = requests.get(url, headers=headers, timeout=5) if response.status_code < 400: print(f"✅ {service_name}: API working (HTTP {response.status_code})") else: print(f"❌ {service_name}: API failed (HTTP {response.status_code}) - {response.reason}") print(f" URL: {url}") print(f" Headers: {headers}") except Exception as e: print(f"❌ {service_name}: Connection failed - {e}") def test_jellyfin(): """Test Jellyfin with special auth header""" api_key = os.getenv('JELLYFIN_API_KEY') if not api_key: print("❌ Jellyfin: No API key found") return url = "http://localhost:8096/System/Info" headers = {'Authorization': f'MediaBrowser Token={api_key}'} try: response = requests.get(url, headers=headers, timeout=5) if response.status_code < 400: print(f"✅ Jellyfin: API working (HTTP {response.status_code})") else: print(f"❌ Jellyfin: API failed (HTTP {response.status_code}) - {response.reason}") except Exception as e: print(f"❌ Jellyfin: Connection failed - {e}") def test_qbittorrent(): """Test qBittorrent with login""" username = os.getenv('QBITTORRENT_USERNAME') password = os.getenv('QBITTORRENT_PASSWORD') if not username or not password: print("❌ qBittorrent: No credentials found") return try: session = requests.Session() login_response = session.post('http://localhost:8080/api/v2/auth/login', data={'username': username, 'password': password}, timeout=5) if login_response.status_code == 200: version_response = session.get('http://localhost:8080/api/v2/app/version', timeout=5) if version_response.status_code == 200: print(f"✅ qBittorrent: API working (HTTP {version_response.status_code})") else: print(f"❌ qBittorrent: Version check failed (HTTP {version_response.status_code})") else: print(f"❌ qBittorrent: Login failed (HTTP {login_response.status_code})") except Exception as e: print(f"❌ qBittorrent: Connection failed - {e}") if __name__ == "__main__": print("🔍 Testing API Authentication:") print("=" * 50) # Test *arr services test_service_api('sonarr', 'http://localhost:8989', '/api/v3/system/status') test_service_api('radarr', 'http://localhost:7878', '/api/v3/system/status') test_service_api('lidarr', 'http://localhost:8686', '/api/v1/system/status') # Test Jellyfin with special header test_jellyfin() # Test qBittorrent with login test_qbittorrent() # Test services that work test_service_api('jellyseerr', 'http://localhost:5055', '/api/v1/status') test_service_api('jackett', 'http://localhost:9117', '/api/v2.0/server/config')