Error code: 401 - {'error': {'code': '', 'message': '无效的令牌 (request id: 20260413...
错误信息
⚠ [llm] reminder extract FAIL | AuthenticationError: Error code: 401 - {'error': {'code': '', 'message': '无效的令牌 (request id: 20260413082630137295894sRHBk3Nl)', 'type': 'new_api_error'}}
⚠ [llm] chat FAIL | 0.3s | AuthenticationError: Error code: 401 - {'error': {'code': '', 'message': '无效的令牌 (request id: 20260413082630474380080S0NJvpHZ)', 'type': 'new_api_error'}} | input=你好
AI 解决方案
报错分析报告
1.错误翻译
原文:AuthenticationError: Error code: 401 | Invalid token
译文:认证错误:错误代码401 | 无效令牌
2. 原因分析
reminder extract和chat接口时,身份验证失败 3. 解决方案
import requests
import os
from dotenv import load_dotenv
# 加载环境变量(推荐用.env文件存令牌)
load_dotenv()
VALID_TOKEN = os.getenv("DMXAPI_TOKEN") # 替换为有效令牌
def call_dmxapi_chat(input_text):
url = "https://api.dmxapi.com/v1/chat" # 实际接口地址以文档为准
headers = {
"Authorization": f"Bearer {VALID_TOKEN}",
"Content-Type": "application/json"
}
payload = {"input": input_text}
try:
response = requests.post(url, headers=headers, json=payload, timeout=10)
response.raise_for_status() # 主动抛出HTTP错误
return response.json()
except requests.exceptions.HTTPError as e:
print(f"API错误: {e.response.status_code} - {e.response.text}")
# 使用示例
result = call_dmxapi_chat("你好")
print(result)
4. 预防措施
/auth/verify接口),过期前自动刷新 python-dotenv(管理环境变量)、Postman(预检接口权限)注:需替换
VALID_TOKEN为DMXAPI平台颁发的有效令牌,接口地址以官方文档为准。