Hello everyone,
I’m encountering a consistent issue when trying to connect to the Label Studio API from a Python script using the label-studio-sdk
. I’m repeatedly getting a 401 Client Error: Unauthorized
with the detail “Invalid token,” despite having tried all known troubleshooting steps.
Problem Details:
- Label Studio URL:
http://127.0.0.1:8087
(server runs with--port 8087 --internal-host 127.0.0.1
). - Python script output:
- The initial connection test succeeds:
Server responds at http://127.0.0.1:8087 with status code 200 (OK). Seems promising!
- However, when attempting to access the API (e.g.,
client.get_projects()
), I receive anHTTPError: 401 Client Error: Unauthorized for url: http://127.0.0.1:8087/api/projects?page_size=10000000
with the content{ "detail": "Invalid token." }
.
- The initial connection test succeeds:
Steps I’ve Already Taken (Unsuccessfully):
- Label Studio Server Verification:
- The server is running and accessible via browser at
http://127.0.0.1:8087
. - The Python script receives a
200 OK
response from the base URL.
- Firewall/Connection Issues:
- Configured Outbound Rules in Windows Defender Firewall to allow Python to connect to port 8087.
Test-NetConnection
works correctly.- The
200 OK
response confirms that the basic connection works and there are no network blocks.
- API Key (Multiple Careful Attempts):
- I have copied and pasted the API Key dozens of times with extreme care, including using the “Copy” button in the UI and pasting into a plain text editor to avoid spaces or hidden characters.
- I deleted all existing tokens in “Account & Settings” → “Access Tokens” and generated a completely new token for my user (who has an Owner role).
- No environment variables (like
HOST
) are set to interfere with server binding. - I even tried creating a brand new user account in Label Studio and generating a fresh API Key for that user, but still encountered the same “Invalid token” error.
- The SDK example code is functionally identical to what I’m using.
- Label Studio SDK Version:
pip install --upgrade label-studio-sdk
has been performed.
Despite all these checks, the “Invalid token” error persists. It seems the issue is specifically with the API Key recognition, even though it appears to be copied correctly from the Label Studio UI.
Any help or further guidance would be greatly appreciated.