From 5831eb8a6a01c0c9399c76260fb713dc74278445 Mon Sep 17 00:00:00 2001 From: shaw Date: Tue, 23 Dec 2025 17:14:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DClaude=20OAuth=20token?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E6=97=B6authorization=20code=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原代码中 `parts` 变量被创建但从未使用,导致 `len(parts) == 0` 永远为 true,使得即使成功从 `code#state` 格式中分割出 authCode, 最后也会被覆盖为原始的完整字符串。 这导致传递给Claude Token端点的code包含了 `#state` 部分, Claude返回 "Invalid 'code' in request" 错误。 --- .../internal/repository/claude_oauth_service.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/backend/internal/repository/claude_oauth_service.go b/backend/internal/repository/claude_oauth_service.go index 8cc4bce2..262d8e8b 100644 --- a/backend/internal/repository/claude_oauth_service.go +++ b/backend/internal/repository/claude_oauth_service.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "net/url" + "strings" "time" "sub2api/internal/pkg/oauth" @@ -139,20 +140,12 @@ func (s *claudeOAuthService) GetAuthorizationCode(ctx context.Context, sessionKe func (s *claudeOAuthService) ExchangeCodeForToken(ctx context.Context, code, codeVerifier, state, proxyURL string) (*oauth.TokenResponse, error) { client := createReqClient(proxyURL) + // Parse code which may contain state in format "authCode#state" authCode := code codeState := "" - if len(code) > 0 { - parts := make([]string, 0, 2) - for i, part := range []rune(code) { - if part == '#' { - authCode = code[:i] - codeState = code[i+1:] - break - } - } - if len(parts) == 0 { - authCode = code - } + if idx := strings.Index(code, "#"); idx != -1 { + authCode = code[:idx] + codeState = code[idx+1:] } reqBody := map[string]any{