From 967c54ae320020b0fa8551e2390fab5abe916cae Mon Sep 17 00:00:00 2001 From: Sakurasan <26715255+Sakurasan@users.noreply.github.com> Date: Tue, 18 Aug 2026 00:51:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86:=20=E9=89=B4=E6=9D=83?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20Anthropic=20x-api-key=20=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude Code / Anthropic SDK 用 x-api-key 头而不是 Authorization: Bearer,缺失 Authorization 时回退读取 x-api-key,错误文案同步提示两种方式 Co-Authored-By: Claude --- server/internal/proxy/gateway.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/internal/proxy/gateway.go b/server/internal/proxy/gateway.go index 4369158..a931196 100644 --- a/server/internal/proxy/gateway.go +++ b/server/internal/proxy/gateway.go @@ -135,8 +135,12 @@ func (g *Gateway) Auth(c *gin.Context) { auth := c.GetHeader("Authorization") key := strings.TrimPrefix(auth, "Bearer ") key = strings.TrimSpace(key) + if key == "" { + // Anthropic 客户端(Claude Code / SDK)用 x-api-key 头而不是 Authorization + key = strings.TrimSpace(c.GetHeader("x-api-key")) + } if !apikey.Valid(key) { - apiError(c, http.StatusUnauthorized, "invalid_api_key", "Invalid API key format. Expected: Bearer sk-...") + apiError(c, http.StatusUnauthorized, "invalid_api_key", "Invalid API key format. Expected: Authorization: Bearer sk-... or x-api-key: sk-...") c.Abort() return }