fix: Bearer token authentication header format

This commit is contained in:
zhanggangyong
2026-01-30 18:19:05 +00:00
parent 703f4b4c21
commit 56668402c3
3 changed files with 124 additions and 1 deletions
+4 -1
View File
@@ -141,7 +141,10 @@ class AgentCodeGenerator:
## 重要提示
1. 如果 URL 像 `https://r.jina.ai/` 这样需要将参数拼接到路径中(如 `https://r.jina.ai/{{target_url}}`),请正确处理 URL 拼接
2. 如果认证是 bearer token,使用环境变量 `TOOL_API_KEY` 获取
2. **认证方式处理**:
- 如果认证类型是 `bearer`,必须使用 `Authorization: Bearer <token>` 格式的 header(不是 X-API-Key!)
- 从环境变量 `TOOL_API_KEY` 获取 token:`os.getenv("TOOL_API_KEY")`
- 示例:`headers = {{"Authorization": f"Bearer {{os.getenv('TOOL_API_KEY')}}"}}`
3. 函数必须是 async def,返回 JSON 字符串
4. 使用 httpx 作为 HTTP 客户端
5. 包含完整的错误处理
@@ -0,0 +1,54 @@
{
"tool_ref_id": "tool-jina_reader_v8-fcc2e021",
"name": "jina_reader_v8",
"description": "使用 Jina Reader API 爬取网页内容",
"config": {
"name": "jina_reader_v8",
"description": "使用 Jina Reader API 爬取网页内容",
"url": "https://r.jina.ai/",
"method": "POST",
"headers": null,
"auth": {
"type": "bearer",
"key": "jina_e26dc30420a44a1e859216528065b203TkMRmsoz-FgMDQC5FZX9jr5oF2CI",
"token": "jina_e26dc30420a44a1e859216528065b203TkMRmsoz-FgMDQC5FZX9jr5oF2CI",
"username": null,
"password": null,
"in": "header",
"name": "X-API-Key"
},
"request_params": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "要爬取的网页URL"
}
},
"required": [
"url"
]
},
"input_schema": {
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "要爬取的网页URL"
}
},
"required": [
"url"
]
},
"request_body": null,
"response_mapping": null,
"timeout": 30,
"retry": null
},
"user_id": "test-user",
"tenant_id": null,
"created_at": "2026-01-30T18:18:49.280650",
"updated_at": "2026-01-30T18:18:49.280660",
"status": "created"
}
@@ -0,0 +1,66 @@
"""
工具: jina_reader_v8
描述: 使用 Jina Reader API 爬取网页内容
"""
import os
import json
from typing import Optional, Any
import httpx
async def jina_reader_v8(url: str) -> str:
"""
使用 Jina Reader API 爬取网页内容
Args:
url: 要爬取的网页URL
Returns:
JSON 字符串格式的响应结果
"""
try:
api_token = os.getenv("TOOL_API_KEY")
if not api_token:
return json.dumps({
"error": "Missing API token",
"message": "TOOL_API_KEY environment variable is not set"
}, ensure_ascii=False)
# 将目标 URL 拼接到 API 路径中
api_url = f"https://r.jina.ai/{url}"
headers = {
"Authorization": f"Bearer {api_token}"
}
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(
api_url,
headers=headers
)
response.raise_for_status()
# 尝试解析为 JSON,如果失败则返回文本内容
try:
result = response.json()
except json.JSONDecodeError:
result = {"content": response.text}
return json.dumps(result, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return json.dumps({
"error": "HTTP error",
"status_code": e.response.status_code,
"message": str(e),
"response": e.response.text
}, ensure_ascii=False)
except httpx.RequestError as e:
return json.dumps({
"error": "Request error",
"message": str(e)
}, ensure_ascii=False)
except Exception as e:
return json.dumps({
"error": "Unexpected error",
"message": str(e)
}, ensure_ascii=False)