Huggingface-cli login issue
Firstly I apologize if it’s really basic or trivial, but I don’t seem to understand how to loginI’m trying to login with the huggingface-cli login and it keeps giving me the followinghuggingface-cli login --token <THE_TOKEN>
The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.
Traceback (most recent call last):
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/utils/_http.py", line 406, in REDACTED_TOKEN_for_status
    response.raise_for_status()
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/api/whoami-v2

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/REDACTED_TOKEN.py", line 1633, in whoami
    REDACTED_TOKEN_for_status(r)
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/utils/_http.py", line 477, in REDACTED_TOKEN_for_status
    raise _format(HfHubHTTPError, str(e), response) from e
huggingface_hub.errors.HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/api/whoami-v2 (Request ID: Root=1-678e5498-0b3b190044e69e0613e6cff6;d4ef20db-e33b-42cc-ac5c-5643c9e9b530)

Invalid username or password.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/bin/huggingface-cli", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/commands/huggingface_cli.py", line 57, in main
    service.run()
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/commands/user.py", line 153, in run
    login(
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/utils/_deprecation.py", line 101, in inner_f
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/utils/_deprecation.py", line 31, in inner_f
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/_login.py", line 126, in login
    _login(token, add_to_git_credential=add_to_git_credential)
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/_login.py", line 404, in _login
    token_info = whoami(token)
                 ^^^^^^^^^^^^^
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/appsinacup/AlexStuff/ComfyUI/imgaienv/lib/python3.12/site-packages/huggingface_hub/REDACTED_TOKEN.py", line 1635, in whoami
    raise HTTPError(
requests.exceptions.HTTPError: Invalid user token. If you didn't pass a user token, make sure you are properly logged in by executing `huggingface-cli login`, and if you did pass a user token, double-check it's correct.also the token seems to work when testing it withcurl -i https://huggingface.co/api/whoami-v2 \
  -H "Authorization: Bearer <THE TOKEN>"
HTTP/2 200
content-type: application/json; charset=utf-8
content-length: 382
date: Mon, 20 Jan 2025 14:08:15 GMT
x-powered-by: huggingface-moon
cross-origin-opener-policy: same-origin
referrer-policy: strict-origin-when-cross-origin
x-request-id: Root=1-678e58cf-35223b3a7ca3d73215320b2d
access-control-allow-origin: https://huggingface.co
vary: Origin
access-control-expose-headers: X-Repo-Commit,X-Request-Id,X-Error-Code,X-Error-Message,X-Total-Count,ETag,Link,Accept-Ranges,Content-Range,X-Xet-Access-Token,X-Xet-Token-Expiration,X-Xet-Refresh-Route,X-Xet-Cas-Url,X-Xet-Hash
etag: W/"17e-uXTz1hbj/22y/TAql9VBBdqElGo"
x-cache: Miss from cloudfront
via: 1.1 e76c434a546615d1c8c0e096c588e7d4.cloudfront.net (CloudFront)
x-amz-cf-pop: OTP50-P2
x-amz-cf-id: 4T2TKFmHXP4ma4JeDw0sy4znD0ocV6p9kuv31krBlI0z4kSYbEGaaQ==

{"type":"user","id":"63dcf661f37111482522cda7","name":"propolys","fullname":"Alex","email":"alexander_00012@yahoo.com","emailVerified":true,"canPay":false,"periodEnd":null,"isPro":false,"avatarUrl":"/avatars/eb4ec70ff9803cb66fab23117ff45b02.svg","orgs":[],"auth":{"type":"access_token","accessToken":{"displayName":"Comfyread","role":"read","createdAt":"2025-01-20T13:02:07.690Z"}}}Thank you for any help, and sorry again if it’s a noob questionSystem info- huggingface_hub version: 0.27.1
- Platform: Linux-6.8.0-51-generic-aarch64-with-glibc2.39
- Python version: 3.12.3
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Running in Google Colab Enterprise ?: No
- Token path ?: /home/appsinacup/.cache/huggingface/token
- Has saved token ?: False
- Configured git credential helpers:
- FastAI: N/A
- Tensorflow: N/A
- Torch: 2.6.0.dev20241224+cu126
- Jinja2: 3.1.3
- Graphviz: N/A
- keras: N/A
- Pydot: 3.0.3
- Pillow: 11.1.0
- REDACTED_TOKEN: N/A
- gradio: N/A
- tensorboard: N/A
- numpy: 1.26.4
- pydantic: 2.10.3
- aiohttp: 3.11.10
- ENDPOINT: https://huggingface.co
- HF_HUB_CACHE: /home/appsinacup/.cache/huggingface/hub
- HF_ASSETS_CACHE: /home/appsinacup/.cache/huggingface/assets
- HF_TOKEN_PATH: /home/appsinacup/.cache/huggingface/token
- HF_STORED_TOKENS_PATH: /home/appsinacup/.cache/huggingface/stored_tokens
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_ENABLE_HF_TRANSFER: False
- HF_HUB_ETAG_TIMEOUT: 10
- HF_HUB_DOWNLOAD_TIMEOUT: 10
2025-01-20T14:40:45Z
huggingface_hub is also the latest version, and the procedure doesn’t seem to be wrong. Even if the token itself is fine-grained rather than read or write, there shouldn’t be a problem with logging in…If it’s an SSL error, the authentication by requests won’t go through either.It’s not impossible that there is a common case where HF_TOKEN is set somewhere and that takes priority, but it’s not in the list of environment variables. What is this phenomenon…?github.com/huggingface/huggingface_hub401 Client Error - Issue with logging with Huggingface CLI/codeopened09:23PM - 01 Oct 24 UTCclosed08:20AM - 15 Oct 24 UTCNamburiSrinathbug### Describe the bug

I got access to Llama-3.2 but when I tried to access the…model, I got the 401 Error.

```
OSError: You are trying to access a gated repo.
Make sure to have access to it at https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct.
401 Client Error. (Request ID: Root=1-66fc667a-79545df845c9253539ea509f;f6f84d32-d328-45e5-b78d-96461057d7d0)

Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/resolve/main/config.json.
Access to model meta-llama/Llama-3.2-1B-Instruct is restricted. You must have access to it and be authenticated to access it. Please log in.
```

Now this means either I don't have access to 3.2B or I am not logged in. I got approved access, so it's not the first one (more about this in Reproduction).

I added my HF token key using `huggingface-cli login`, but I am still facing the same issue after that.

I tried to debug and found that the command `huggingface-cli whoami` gives me this error - 
`Invalid user token. If you didn't pass a user token, make sure you are properly logged in by executing huggingface-cli login, and if you did pass a user token, double-check it's correct. {"error":"Invalid username or password."}`

So, basically even if I login (either via CLI or in code), I am getting this error

```
from huggingface_hub import login
login(token = "xxx")
```

**Note:** The only change I made (from yesterday when Llama-2 is working to today) is to change some environment variables i.e
```
export HF_HOME=xxx
export HF_ASSETS_CACHE=xxx
export XDG_CACHE_HOME=xxx
export HF_TOKEN=xxx
export HF_HUB_CACHE=xxx
```

### Reproduction

I also experimented with `Llama-2-7B` (which I used till yesterday and it was fine) but was getting the same `GatedError`.

```
from transformers import AutoTokenizer
import transformers
import torch

from huggingface_hub import login
login(token = "xxx")

model = "meta-llama/Llama-2-7b-chat-hf"

tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = transformers.pipeline(
    "text-generation",
    model=model,
    torch_dtype=torch.float16,
    device_map="auto",
)

sequences = pipeline(
    'I liked "Breaking Bad" and "Band of Brothers". Do you have any recommendations of other shows I might like?\n',
    do_sample=True,
    top_k=10,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id,
    max_length=200,
)
for seq in sequences:
    print(f"Result: {seq['generated_text']}")
```
### Logs

```
The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.
Token is valid (permission: fineGrained).
Your token has been saved to /qumulo/satya/huggingface/token
Login successful
Traceback (most recent call last):
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/utils/_http.py", line 406, in REDACTED_TOKEN_for_status
    response.raise_for_status()
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/utils/hub.py", line 403, in cached_file
    resolved_file = REDACTED_TOKEN_download(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/utils/_deprecation.py", line 101, in inner_f
    return f(*args, **kwargs)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1232, in REDACTED_TOKEN_download
    return _REDACTED_TOKEN_download_to_cache_dir(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1339, in _REDACTED_TOKEN_download_to_cache_dir
    _raise_on_head_call_error(head_call_error, force_download, local_files_only)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1854, in _raise_on_head_call_error
    raise head_call_error
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1746, in _get_metadata_or_catch_error
    metadata = get_REDACTED_TOKEN_metadata(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1666, in get_REDACTED_TOKEN_metadata
    r = _request_wrapper(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 364, in _request_wrapper
    response = _request_wrapper(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 388, in _request_wrapper
    REDACTED_TOKEN_for_status(response)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/huggingface_hub/utils/_http.py", line 423, in REDACTED_TOKEN_for_status
    raise _format(GatedRepoError, message, response) from e
huggingface_hub.errors.GatedRepoError: 401 Client Error. (Request ID: Root=1-66fc6aa2-51edf73166f9906f2c6e4de2;44b58636-df14-4a1a-bb9c-4d83423f68ed)

Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json.
Access to model meta-llama/Llama-2-7b-chat-hf is restricted. You must have access to it and be authenticated to access it. Please log in.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/qumulo/satya/Compress_Align/scratch_code/pytorch_test.py", line 15, in <module>
    tokenizer = AutoTokenizer.from_pretrained(model)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 864, in from_pretrained
    config = AutoConfig.from_pretrained(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/models/auto/configuration_auto.py", line 1006, in from_pretrained
    config_dict, unused_kwargs = PretrainedConfig.get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/configuration_utils.py", line 567, in get_config_dict
    config_dict, kwargs = cls._get_config_dict(pretrained_model_name_or_path, **kwargs)
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/configuration_utils.py", line 626, in _get_config_dict
    resolved_config_file = cached_file(
  File "/qumulo/satya/anaconda3/envs/prune_llm/lib/python3.9/site-packages/transformers/utils/hub.py", line 421, in cached_file
    raise EnvironmentError(
OSError: You are trying to access a gated repo.
Make sure to have access to it at https://huggingface.co/meta-llama/Llama-2-7b-chat-hf.
401 Client Error. (Request ID: Root=1-66fc6aa2-51edf73166f9906f2c6e4de2;44b58636-df14-4a1a-bb9c-4d83423f68ed)

Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-chat-hf/resolve/main/config.json.
Access to model meta-llama/Llama-2-7b-chat-hf is restricted. You must have access to it and be authenticated to access it. Please log in.
```

### System info

```shell
- huggingface_hub version: 0.25.1
- Platform: Linux-5.15.0-1065-nvidia-x86_64-with-glibc2.35
- Python version: 3.9.19
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Running in Google Colab Enterprise ?: No
- Token path ?: /qumulo/satya/huggingface/token
- Has saved token ?: True
- Configured git credential helpers: 
- FastAI: N/A
- Tensorflow: N/A
- Torch: 2.4.1
- Jinja2: 3.1.4
- Graphviz: N/A
- keras: N/A
- Pydot: N/A
- Pillow: N/A
- REDACTED_TOKEN: N/A
- gradio: N/A
- tensorboard: N/A
- numpy: 1.26.4
- pydantic: 2.9.2
- aiohttp: 3.10.8
- ENDPOINT: https://huggingface.co
- HF_HUB_CACHE: /qumulo/satya/huggingface
- HF_ASSETS_CACHE: /qumulo/satya/huggingface
- HF_TOKEN_PATH: /qumulo/satya/huggingface/token
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_ENABLE_HF_TRANSFER: False
- HF_HUB_ETAG_TIMEOUT: 10
- HF_HUB_DOWNLOAD_TIMEOUT: 10
```github.com/huggingface/huggingface_hubAuthorization header is correct, but the token seems invalidopened07:57AM - 04 Sep 24 UTCDevPatel1412bug### Describe the bug


So my issue is this error, I was working with same 'HF_t…oken' having the write permission and I am working with Mistral Nemo 12B Instruct , the model was working well from last few days without any issue and today suddenly this error appears .
This error appears to be persistant, i have refresh the token and also tried with other model too and also check the hugging face interface api server status as well, but the issue remains the same.

### **Issue** 
**BadRequestError
huggingface_hub.utils._errors.BadRequestError:  (Request ID: Mq1mDWKbogI0AleJS0HJM)**

**Bad request:
Authorization header is correct, but the token seems invalid**

This is my  ```app.py``` file, and I have not modified or added any other files:

```
from huggingface_hub import InferenceClient
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Authenticate with Hugging Face
HFT = os.getenv('HF_TOKEN')
client = InferenceClient(model="mistralai/Mistral-Nemo-Instruct-2407", token=HFT) 

#results
response = ""
    for message in client.chat_completion(
        messages=[system_role, user_prompt],
        max_tokens=3000,
        stream=True,
        temperature=0.35,
    ):
        response += message.choices[0].delta.content
```



### Reproduction

_No response_

### Logs

```shell
Traceback (most recent call last):
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\huggingface_hub\utils\_errors.py", line 304, in REDACTED_TOKEN_for_status
    response.raise_for_status()
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\requests\models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api-inference.huggingface.co/models/mistralai/Mistral-Nemo-Instruct-2407/v1/chat/completions

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\flask\app.py", line 1498, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\flask\app.py", line 1476, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\flask\app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\flask\app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Admin\Documents\_app_30082024\envo\lib\site-packages\flask\app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\flask\app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
  File "C:\Users\Admin\Documents\app_30082024\app5.py", line 78, in process_file 
    per_data = Model_PersonalDetails_Output(resume, client)
  File "C:\Users\Admin\Documents\app_30082024\utility\llm_generate.py", line 127, in Model_PersonalDetails_Output
    for message in client.chat_completion(
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\huggingface_hub\inference\_client.py", line 837, in chat_completion
    data = self.post(
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\huggingface_hub\inference\_client.py", line 304, in post
    REDACTED_TOKEN_for_status(response)
  File "C:\Users\Admin\Documents\app_30082024\envo\lib\site-packages\huggingface_hub\utils\_errors.py", line 358, in REDACTED_TOKEN_for_status
    raise BadRequestError(message, response=response) from e
huggingface_hub.utils._errors.BadRequestError:  (Request ID: Mq1mDWKbogI0AleJS0HJM)

Bad request:
Authorization header is correct, but the token seems invalid
```


### System info

```shell
- huggingface_hub version: 0.24.6
- Platform: Windows-10-10.0.19045-SP0
- Python version: 3.10.10
- Running in iPython ?: No
- Running in notebook ?: No
- Running in Google Colab ?: No
- Token path ?: C:\Users\Admin\.cache\huggingface\token
- Has saved token ?: True
- FastAI: N/A
- Tensorflow: N/A
- Torch: 2.4.0
- Jinja2: 3.1.4
- Graphviz: N/A
- keras: N/A
- Pydot: N/A
- Pillow: 10.4.0
- REDACTED_TOKEN: N/A
- gradio: N/A
- tensorboard: N/A
- numpy: 1.26.4
- pydantic: N/A
- aiohttp: N/A
- ENDPOINT: https://huggingface.co
- HF_HUB_CACHE: C:\Users\Admin\.cache\huggingface\hub
- HF_ASSETS_CACHE: C:\Users\Admin\.cache\huggingface\assets
- HF_TOKEN_PATH: C:\Users\Admin\.cache\huggingface\token
- HF_HUB_OFFLINE: False
- HF_HUB_DISABLE_TELEMETRY: False
- HF_HUB_DISABLE_PROGRESS_BARS: None
- HF_HUB_DISABLE_SYMLINKS_WARNING: False
- HF_HUB_DISABLE_EXPERIMENTAL_WARNING: False
- HF_HUB_DISABLE_IMPLICIT_TOKEN: False
- HF_HUB_ENABLE_HF_TRANSFER: False
- HF_HUB_ETAG_TIMEOUT: 10
- HF_HUB_DOWNLOAD_TIMEOUT: 10
```
2025-01-21T11:36:59Z
i seemed to have in ~/.netrc credentials forhuggingface.co, which i think they might have overridden my Authorization header.a smiplerm ~/.netrcdid the trick
2025-01-21T23:37:30Z
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.