Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Options

Persistent 401 Unauthorized Error with Python API Keys Despite All Checks

hari_haran541hari_haran541 Member Posts: 6

Hello everyone,

I'm trying to use the official onshape-client library for Python to connect to the Onshape API, but I'm stuck on a persistent 401 Unauthorized error. My goal is simply to authenticate and eventually create a document, but I cannot get past the initial connection step.

I've followed all the standard troubleshooting steps but the server continues to reject my credentials. I'm hoping someone might have an idea what I could be missing.

What I've Already Tried

  1. Set Correct Permissions: I have ensured the active keys have both Read and Write permissions enabled.
  2. Confirmed My Base URL: My account is on the standard free plan and my browser URL is exactly https://cad.onshape.com. This is correctly set in my script.
  3. Performed a "Clean" Copy/Paste: I've used the "Notepad Test" by pasting the keys into a plain text editor first before moving them to my script to ensure no hidden formatting characters are included.
  4. Synced My System Clock: I have forced a sync of my Windows clock with the internet time server to ensure there is no time-skew issue.
  5. Isolated the Problem: I am using a minimal test script (included below) that only attempts to authenticate and perform a single GET request.

Test Script

This is the exact code I am running. It uses debug logging to show the raw request details. Even with this script, the authentication fails.

Python

import logging
import sys
from onshape_client.client import Client
from onshape_client.oas.exceptions import ApiException


API_KEY = "My_key"
API_SECRET = "My_key"
BASE_URL = "https://cad.onshape.com"



def check_connection_with_debug():
print("Attempting to connect with DEBUG LOGGING ENABLED...")
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

client = Client(configuration={
"base_url": BASE_URL,
"api_key": API_KEY,
"api_secret": API_SECRET
})

try:
print("\n--- Performing test call to list documents ---")
# A simple, read-only call to test the connection
client.documents_api.get_documents(limit=1)

print("\n" + "="*40)
print("SUCCESS! Authentication is correct.")
print("="*40 + "\n")

except ApiException as e:
print("\n" + "="*40)
print(" FAILED! Authentication was rejected by the server.")
print(f"This was a {e.status} error. See the log above for details.")
print("="*40 + "\n")

if __name__ == '__main__':
if "PASTE_YOUR" in API_KEY or "PASTE_YOUR" in API_SECRET:
print("ERROR: You have not replaced the placeholder API keys in the script.")
else:
check_connection_with_debug()

My Question

Even after all these steps, the script consistently fails with a 401 Unauthorized error.

Does anyone have any ideas for other things I could check? Is there any other system setting, account configuration, or network issue that could lead to a valid API key being rejected?

Any help or suggestion would be greatly appreciated. Thank you!

Comments

Sign In or Register to comment.