# httpx.HTTPStatusError: 对URL 'https://api.example.com/v1/restricted' 的客户端错误 '403 禁止访问'

- **ID:** `python/httpx-httperror-403-forbidden`
- **领域:** python
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

服务器拒绝访问，因为权限不足，例如缺少API密钥或IP白名单。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Include the required API key in headers** (95% 成功率)
   ```
   headers = {'Authorization': 'Bearer your-api-key'}
httpx.get('https://api.example.com/v1/restricted', headers=headers)
   ```
2. **Check if the IP address is whitelisted on the server** (80% 成功率)
   ```
   import socket
my_ip = socket.gethostbyname(socket.gethostname())
print(my_ip)  # then verify with server admin
   ```

## 无效尝试

- **Re-sending the request without any authentication headers** — The server still requires valid credentials. (95% 失败率)
- **Using a different HTTP method (e.g., POST instead of GET)** — The issue is authorization, not method. (90% 失败率)
