python requests访问报403 "Invalid username/password."

本文介绍了一种常见的Python requests库访问REST API时出现403错误的问题,并详细解释了如何通过检查和移除.netrc文件来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用python requests访问一个rest api时,总是报错:

403
{"detail":"Invalid username/password."}

换成curl访问就没有问题。

为什么会这样呢?

把requests的请求header print出啦,看到加了authorization,但是代码并没有使用任何认证。

{'Content-Length': '650', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'user-agent': 'a', 'Connection': 'keep-alive',  'content-type': 'application/json', 'Authorization': 'Basic dGVzdDoxMjM='}

猜测和机器环境有关系。用户认证信息应该是存在某个文件中的吧?

用strace看一下打开了哪些文件

strace python test.py

open("/usr/lib64/python2.7/shlex.py", O_RDONLY) = 4
open("/usr/lib64/python2.7/shlex.pyc", O_RDONLY) = 5
open("/root/.netrc", O_RDONLY)          = 3
open("/usr/lib64/python2.7/encodings/idna.py", O_RDONLY) = 3
open("/usr/lib64/python2.7/encodings/idna.pyc", O_RDONLY) = 4
open("/usr/lib64/python2.7/stringprep.py", O_RDONLY) = 4

访问了一个可疑文件 ~/.netrc

$ cat ~/.netrc
default login test password 123

把这个文件拿掉,接口访问就没有报错了。

查一下requests的文档,requests果然会读取.netrc文件
http://docs.python-requests.org/en/master/user/authentication/?highlight=netrc

(caption-env) root@ubuntu:~/models/MobileVLM-1.7B# export HF_ENDPOINT=https://hf-mirror.com (caption-env) root@ubuntu:~/models/MobileVLM-1.7B# huggingface-cli download MobileVLM/MobileVLM-1.7B-GGUF \ --include "mmproj-model-f16.gguf" \ --local-dir ./models Traceback (most recent call last): File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 409, in hf_raise_for_status response.raise_for_status() File "/root/caption-env/lib/python3.10/site-packages/requests/models.py", line 1026, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://hf-mirror.com/api/models/MobileVLM/MobileVLM-1.7B-GGUF/revision/main The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/root/caption-env/bin/huggingface-cli", line 8, in <module> sys.exit(main()) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/commands/huggingface_cli.py", line 59, in main service.run() File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/commands/download.py", line 153, in run print(self._download()) # Print path to downloaded files File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/commands/download.py", line 187, in _download return snapshot_download( File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/_snapshot_download.py", line 245, in snapshot_download raise api_call_error File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/_snapshot_download.py", line 165, in snapshot_download repo_info = api.repo_info(repo_id=repo_id, repo_type=repo_type, revision=revision) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/hf_api.py", line 2844, in repo_info return method( File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/hf_api.py", line 2629, in model_info hf_raise_for_status(r) File "/root/caption-env/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 459, in hf_raise_for_status raise _format(RepositoryNotFoundError, message, response) from e huggingface_hub.errors.RepositoryNotFoundError: 401 Client Error. (Request ID: Root=1-686e1515-3beb98ac6fa889bc6c585014;e4999aad-8bdf-412a-b58e-6cf930bed578) Repository Not Found for url: https://hf-mirror.com/api/models/MobileVLM/MobileVLM-1.7B-GGUF/revision/main. Please make sure you specified the correct `repo_id` and `repo_type`. If you are trying to access a private or gated repo, make sure you are authenticated. For more details, see https://huggingface.co/docs/huggingface_hub/authentication Invalid username or password.
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值