I'm having an error message working with my User access tokens

Hi, I created a new token with write access but still getting error HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/api/models/mistralai/Mistral-7B-Instruct-v0.2?expand=inferenceProviderMapping Invalid credentials in Authorization header

1 Like

Hmm… Try doing triage like:

import os, requests

os.environ["HF_TOKEN"] = "hf_***your_token***"
TOKEN = os.getenv("HF_TOKEN", "").strip()
HDR = {"Authorization": f"Bearer {TOKEN}"} if TOKEN else {}

WHOAMI = "https://huggingface.co/api/whoami-v2"
MODEL  = "https://huggingface.co/api/models/mistralai/Mistral-7B-Instruct-v0.2"

r1 = requests.get(WHOAMI, headers=HDR if TOKEN else None, timeout=15)
r2 = requests.get(MODEL, timeout=15)                         # public, no auth
r3 = requests.get(MODEL, headers=HDR if TOKEN else None, timeout=15)

print("whoami-v2 with token:", r1.status_code)
print("model metadata (no auth):", r2.status_code)
print("model metadata (with token):", r3.status_code)

if TOKEN and r1.status_code == 401:
    print("=> Bad token or bad Authorization header; rotate token or fix to 'Bearer <token>'.")
elif r2.ok and TOKEN and r3.status_code == 401:
    print("=> Your header breaks a public call; header malformed or proxy strips Authorization. Remove header or fix it.")
else:
    print("=> Looks OK for Hub metadata.")