Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Quick script to verify your Supabase API key format and connection. | |
| """ | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| url = os.getenv("SUPABASE_URL") | |
| key = os.getenv("SUPABASE_SERVICE_KEY") | |
| print("=" * 70) | |
| print("Supabase API Key Verification") | |
| print("=" * 70) | |
| print() | |
| if not url: | |
| print("β SUPABASE_URL is not set in .env file") | |
| exit(1) | |
| if not key: | |
| print("β SUPABASE_SERVICE_KEY is not set in .env file") | |
| exit(1) | |
| # Clean the key | |
| key = key.strip() | |
| print(f"π SUPABASE_URL: {url[:30]}...") | |
| print(f"π SUPABASE_SERVICE_KEY: {key[:20]}...{key[-10:] if len(key) > 30 else ''} ({len(key)} chars)") | |
| print() | |
| # Check key format | |
| issues = [] | |
| if not key.startswith("eyJ"): | |
| issues.append("β Key doesn't start with 'eyJ' (not a JWT token)") | |
| if len(key) < 100: | |
| issues.append(f"β Key is too short ({len(key)} chars, expected ~200+)") | |
| if len(key) > 500: | |
| issues.append(f"β οΈ Key is unusually long ({len(key)} chars)") | |
| if " " in key or "\n" in key or "\t" in key: | |
| issues.append("β Key contains whitespace (spaces, newlines, tabs)") | |
| if key.startswith('"') or key.endswith('"'): | |
| issues.append("β Key is wrapped in quotes (remove quotes from .env)") | |
| if key.startswith("'") or key.endswith("'"): | |
| issues.append("β Key is wrapped in single quotes (remove quotes from .env)") | |
| if issues: | |
| print("β οΈ Issues found with API key format:") | |
| for issue in issues: | |
| print(f" {issue}") | |
| print() | |
| else: | |
| print("β Key format looks good!") | |
| print() | |
| # Try to connect | |
| print("π Testing connection to Supabase...") | |
| try: | |
| from supabase import create_client | |
| client = create_client(url, key) | |
| # Try a simple query | |
| try: | |
| client.table("admin_rules").select("id").limit(0).execute() | |
| print("β Connection successful! API key is valid.") | |
| print() | |
| print("π‘ Next steps:") | |
| print(" 1. Make sure tables exist (run SQL scripts in Supabase)") | |
| print(" 2. Run: python migrate_sqlite_to_supabase.py") | |
| except Exception as e: | |
| error_str = str(e) | |
| if "Invalid API key" in error_str or "401" in error_str: | |
| print("β Connection failed: Invalid API key") | |
| print() | |
| print("π§ How to fix:") | |
| print(" 1. Go to https://app.supabase.com") | |
| print(" 2. Select your project") | |
| print(" 3. Go to Settings β API") | |
| print(" 4. Find 'service_role' key (NOT 'anon' key)") | |
| print(" 5. Click 'Reveal' to show the full key") | |
| print(" 6. Copy the ENTIRE key (it's very long)") | |
| print(" 7. Update SUPABASE_SERVICE_KEY in .env file") | |
| print(" 8. Make sure NO quotes or spaces around the value") | |
| elif "does not exist" in error_str or "relation" in error_str.lower(): | |
| print("β οΈ Connection works, but table doesn't exist yet") | |
| print(" This is OK - create tables first, then migrate") | |
| else: | |
| print(f"β Connection error: {error_str}") | |
| except ImportError: | |
| print("β Supabase Python client not installed") | |
| print(" Run: pip install supabase") | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| print() | |
| print("=" * 70) | |