Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
ac4371fa
Unverified
Commit
ac4371fa
authored
Feb 04, 2026
by
Wesley Liddick
Committed by
GitHub
Feb 04, 2026
Browse files
Merge pull request #482 from IanShaw027/fix/gateway-compatibility
fix(gemini): 优化 Gemini 接口认证兼容性,支持 Authorization: Bearer
parents
804b6f22
9985c4a3
Changes
1
Hide whitespace changes
Inline
Side-by-side
backend/internal/server/middleware/api_key_auth_google.go
View file @
ac4371fa
...
...
@@ -26,7 +26,7 @@ func APIKeyAuthWithSubscriptionGoogle(apiKeyService *service.APIKeyService, subs
abortWithGoogleError
(
c
,
400
,
"Query parameter api_key is deprecated. Use Authorization header or key instead."
)
return
}
apiKeyString
:=
extractAPIKeyF
romRequest
(
c
)
apiKeyString
:=
extractAPIKeyF
orGoogle
(
c
)
if
apiKeyString
==
""
{
abortWithGoogleError
(
c
,
401
,
"API key is required"
)
return
...
...
@@ -108,25 +108,38 @@ func APIKeyAuthWithSubscriptionGoogle(apiKeyService *service.APIKeyService, subs
}
}
func
extractAPIKeyF
romRequest
(
c
*
gin
.
Context
)
string
{
authHeader
:=
c
.
GetHeader
(
"Authorization"
)
if
authH
ea
d
er
!=
""
{
parts
:=
strings
.
SplitN
(
authHeader
,
" "
,
2
)
if
len
(
parts
)
==
2
&&
parts
[
0
]
==
"Bearer"
&&
strings
.
TrimSpace
(
parts
[
1
])
!=
""
{
return
strings
.
TrimSpace
(
parts
[
1
])
}
//
extractAPIKeyF
orGoogle extracts API key for Google/Gemini endpoints.
// Priority: x-goog-api-key > Authorization: Bearer > x-api-key > query key
// This allows OpenClaw and other clients using B
ea
r
er
auth to work with Gemini endpoints.
func
extractAPIKeyForGoogle
(
c
*
gin
.
Context
)
string
{
// 1) preferred: Gemini native header
if
k
:=
strings
.
TrimSpace
(
c
.
GetHeader
(
"x-goog-api-key"
));
k
!=
""
{
return
k
}
if
v
:=
strings
.
TrimSpace
(
c
.
GetHeader
(
"x-api-key"
));
v
!=
""
{
return
v
// 2) fallback: Authorization: Bearer <key>
auth
:=
strings
.
TrimSpace
(
c
.
GetHeader
(
"Authorization"
))
if
auth
!=
""
{
parts
:=
strings
.
SplitN
(
auth
,
" "
,
2
)
if
len
(
parts
)
==
2
&&
strings
.
EqualFold
(
parts
[
0
],
"Bearer"
)
{
if
k
:=
strings
.
TrimSpace
(
parts
[
1
]);
k
!=
""
{
return
k
}
}
}
if
v
:=
strings
.
TrimSpace
(
c
.
GetHeader
(
"x-goog-api-key"
));
v
!=
""
{
return
v
// 3) x-api-key header (backward compatibility)
if
k
:=
strings
.
TrimSpace
(
c
.
GetHeader
(
"x-api-key"
));
k
!=
""
{
return
k
}
// 4) query parameter key (for specific paths)
if
allowGoogleQueryKey
(
c
.
Request
.
URL
.
Path
)
{
if
v
:=
strings
.
TrimSpace
(
c
.
Query
(
"key"
));
v
!=
""
{
return
v
}
}
return
""
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment