shaare.it

Basic Auth Wrong Credentials

7 Dec 2025

1 min read

Basic Auth wrong credentials

$ python -c "import requests; requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user','wrong'))"
Traceback (most recent call last):
  ...
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url

Why this happens

The server validates credentials; wrong username/password cause 401.

Fix

Verify credentials, rotate passwords, and avoid hard-coding. Use env vars or secret managers.

Wrong code

import requests
requests.get('https://service/auth', auth=('user','wrong'))

Fixed code

import os, requests
user = os.getenv('API_USER')
pwd = os.getenv('API_PASS')
requests.get('https://service/auth', auth=(user, pwd), timeout=10)