fix: use POST /api/token/{id}/key to get full unmasked key for new NewAPI v0.13.2

New version of NewAPI masks token keys with asterisks in list/detail endpoints.
Added get_token_key() method that calls the dedicated endpoint to retrieve
the full key, and integrated it into get_or_create_token() flow.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
朱潮 2026-05-13 12:49:38 +08:00
parent 180e569b09
commit a8f643a10c

View File

@ -338,6 +338,20 @@ class NewAPIProxy:
new_api_user_id=new_api_user_id
)
async def get_token_key(
self,
cookies: Dict[str, str],
new_api_user_id: int,
token_id: int
) -> Dict[str, Any]:
"""Get the full unmasked token key by token ID"""
return await self._request(
"POST",
f"/api/token/{token_id}/key",
cookies=cookies,
new_api_user_id=new_api_user_id
)
async def get_or_create_token(
self,
cookies: Dict[str, str],
@ -351,10 +365,14 @@ class NewAPIProxy:
# data 格式: {'page': 1, 'page_size': 100, 'total': 4, 'items': [...]}
tokens = tokens_result["data"].get("items", [])
if isinstance(tokens, list) and len(tokens) > 0:
# 返回第一个可用的令牌
token = tokens[0]
# Fetch the full unmasked key via dedicated endpoint
key_result = await self.get_token_key(cookies, new_api_user_id, token["id"])
if key_result.get("success") and key_result.get("data"):
token["key"] = key_result["data"]["key"]
return {
"success": True,
"data": tokens[0],
"data": token,
"message": "使用现有令牌"
}
@ -370,9 +388,14 @@ class NewAPIProxy:
if tokens_result.get("success") and tokens_result.get("data"):
tokens = tokens_result["data"].get("items", [])
if isinstance(tokens, list) and len(tokens) > 0:
token = tokens[0]
# Fetch the full unmasked key
key_result = await self.get_token_key(cookies, new_api_user_id, token["id"])
if key_result.get("success") and key_result.get("data"):
token["key"] = key_result["data"]["key"]
return {
"success": True,
"data": tokens[0],
"data": token,
"message": "已创建新令牌"
}