`token` parameter not respected for `AutoModel`
Author: squidarth
Pull request: no

### System Info

transformers version: 4.37.2

(`transformers-cli env` errored out for me)


### Who can help?

@ArthurZucker @younesbelkada 

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Hi there,

I am trying to use https://huggingface.co/jinaai/jina-embeddings-v2-base-en, and noticed the following problem:

When using the `token` parameter on `AutoModel`, I get the "You are trying to access a gated repo" error (I have accepted the terms for the model.

I'm working around this by using the `HF_TOKEN` environment variable.

Code:

```
# doesn't work
model = AutoModel.from_pretrained(
    'jinaai/jina-embeddings-v2-base-en', 
    revision="0f472a4cde0e6e50067b8259a3a74d1110f4f8d8", 
    trust_remote_code=True,
    token="MY_HF_TOKEN"
)

# works
os.environ["HF_TOKEN"] = "MY_HF_TOKEN"
model = AutoModel.from_pretrained(
    'jinaai/jina-embeddings-v2-base-en', 
    revision="0f472a4cde0e6e50067b8259a3a74d1110f4f8d8", 
    trust_remote_code=True,
    token="MY_HF_TOKEN"
)
```

thanks for any help here

### Expected behavior

Using the `token` parameter should lead to the same behavior as using the `HF_TOKEN` environment variable.

Comments:
Hey! Thanks for reporting. Could you provide the traceback? 


Hi @ArthurZucker -- it's this:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/utils/_errors.py", line 261, in hf_raise_for_status
    response.raise_for_status()
  File "/usr/local/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/jinaai/jina-embeddings-v2-base-en/resolve/main/tokenizer_config.json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/transformers/utils/hub.py", line 429, in cached_file
    resolved_file = hf_hub_download(
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1346, in hf_hub_download
    raise head_call_error
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1232, in hf_hub_download
    metadata = get_hf_file_metadata(
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/utils/_validators.py", line 118, in _inner_fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/file_download.py", line 1608, in get_hf_file_metadata
    hf_raise_for_status(r)
  File "/usr/local/lib/python3.9/site-packages/huggingface_hub/utils/_errors.py", line 277, in hf_raise_for_status
    raise GatedRepoError(message, response) from e
huggingface_hub.utils._errors.GatedRepoError: 401 Client Error. (Request ID: Root=1-65ba7816-484b8bb66a482da2430cf7b8;65c4fe9b-7a6a-4d35-93d3-e19fdf809080)

The above exception was the direct cause of the following exception:

Cannot access gated repo for url https://huggingface.co/jinaai/jina-embeddings-v2-base-en/resolve/main/tokenizer_config.json.
Repo model jinaai/jina-embeddings-v2-base-en is gated. You must be authenticated to access it.
Traceback (most recent call last):
  File "/app/model/model.py", line 10, in load
    self._model = AutoModel.from_pretrained(
  File "/usr/local/lib/python3.9/site-packages/transformers/models/auto/auto_factory.py", line 560, in from_pretrained
    return model_class.from_pretrained(
  File "/usr/local/lib/python3.9/site-packages/transformers/modeling_utils.py", line 3085, in from_pretrained
    model = cls(config, *model_args, **model_kwargs)
  File "/root/.cache/huggingface/modules/transformers_modules/jinaai/jina-bert-implementation/c41d17d28431712f4b24b52bb83d426d7137a02f/modeling_bert.py", line 1108, in __init__
    self.tokenizer = AutoTokenizer.from_pretrained(config._name_or_path)
  File "/usr/local/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 701, in from_pretrained
    tokenizer_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/transformers/models/auto/tokenization_auto.py", line 534, in get_tokenizer_config
    resolved_config_file = cached_file(
  File "/usr/local/lib/python3.9/site-packages/transformers/utils/hub.py", line 444, in cached_file
    raise EnvironmentError(
OSError: You are trying to access a gated repo.
Make sure to request access at https://huggingface.co/jinaai/jina-embeddings-v2-base-en and pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`.
```


(and just to confirm, I do _not_ get this stacktrace when using the `HF_TOKEN` environment variable (it works great), just when using the `AutoModel(..., token="my_hf_token")` parameter). thanks for the help here!

Hi @squidarth 
I just tried your script on main with a new token + the same model and commit hash and was not able to repro - can you make sure you are using transformers and huggingface_hub from latest pypi ? `pip install -U transformers huggingface_hub`

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.