ZHIWEI666 commited on
Commit
97d5077
·
verified ·
1 Parent(s): ec129c5

Upload router_proxy.py

Browse files
Files changed (1) hide show
  1. router_proxy.py +10 -1
router_proxy.py CHANGED
@@ -95,11 +95,20 @@ async def proxy_download(req_data: ProxyDownloadRequest, db: Session = Depends(g
95
 
96
  target_url = req_data.url
97
 
 
 
 
 
 
 
 
 
98
  # 2. 异步拉取真实 JSON 数据并透传回客户端
99
  try:
100
  # 开启 follow_redirects=True 防止源地址有 302 重定向跳转
101
  async with httpx.AsyncClient(follow_redirects=True) as client:
102
- resp = await client.get(target_url, timeout=30.0)
 
103
 
104
  if resp.status_code != 200:
105
  return JSONResponse(content={"error": f"源文件拉取失败,HTTP状态码: {resp.status_code}"}, status_code=resp.status_code)
 
95
 
96
  target_url = req_data.url
97
 
98
+ # 🚀 核心修复:从环境变量提取 Hugging Face Token,并组装 Authorization 请求头
99
+ # 注意:这里的变量名需与你在 Spaces -> Settings -> Variables and secrets 中设置的名称一致
100
+ hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
101
+
102
+ headers = {}
103
+ if hf_token and "huggingface.co" in target_url:
104
+ headers["Authorization"] = f"Bearer {hf_token}"
105
+
106
  # 2. 异步拉取真实 JSON 数据并透传回客户端
107
  try:
108
  # 开启 follow_redirects=True 防止源地址有 302 重定向跳转
109
  async with httpx.AsyncClient(follow_redirects=True) as client:
110
+ # 🚀 将携带 Token 的 headers 传给 GET 请求,突破私有库 401 封锁
111
+ resp = await client.get(target_url, headers=headers, timeout=30.0)
112
 
113
  if resp.status_code != 200:
114
  return JSONResponse(content={"error": f"源文件拉取失败,HTTP状态码: {resp.status_code}"}, status_code=resp.status_code)