Skip to content

[ai-proxy] 代理 Gemini 时报错认证失败 #2199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
daixijun opened this issue May 8, 2025 · 2 comments
Open

[ai-proxy] 代理 Gemini 时报错认证失败 #2199

daixijun opened this issue May 8, 2025 · 2 comments
Assignees
Labels
area/ai sig/wasm type/bug Something isn't working

Comments

@daixijun
Copy link
Contributor

daixijun commented May 8, 2025

使用 ai-proxy 插件代理 Gemini 服务时报无效的认证凭证错误

ai-proxy 中配置如下

Image

CURL 访问参数如下

curl https://example.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${HIGRESS_API_KEY}" \
  -d '{
      "messages": [
        {
          "role": "user",
          "content": "Who are you?"
        }
      ],
      "model": "gemini-2.0-flash"
    }'
{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

但是使用同一个 KEY,使用 CURL 调用官方接口是可以正常返回

curl "https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent" \
-H 'Content-Type: application/json' \
-H 'x-goog-api-key: GEMINI_API_KEY' \
-d '{
  "contents": [{
    "parts":[{"text": "Who are you?"}]        
    }]
   }'
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "I am a large language model, trained by Google.\n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "avgLogprobs": -3.197540354449302e-05
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 4,
    "candidatesTokenCount": 12,
    "totalTokenCount": 16,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 4
      }
    ],
    "candidatesTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 12
      }
    ]
  },
  "modelVersion": "gemini-2.0-flash"
}

同样使用同一套 KEY, 我切换为 openai 兼容接口,也是可以正常调用

Image

curl https://example.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${HIGRESS_API_KEY}" \
  -d '{
      "messages": [
        {
          "role": "user",
          "content": "Who are you?"
        }
      ],
      "model": "gemini-2.0-flash"
    }'
{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"I am a large language model, trained by Google.\n","role":"assistant"}}],"created":1746698000,"model":"gemini-2.0-flash","object":"chat.completion","usage":{"completion_tokens":12,"prompt_tokens":4,"total_tokens":16}}
@CH3CHO
Copy link
Collaborator

CH3CHO commented May 8, 2025

CC @hanxiantao

@hanxiantao
Copy link
Collaborator

使用 ai-proxy 插件代理 Gemini 服务时报无效的认证凭证错误

ai-proxy 中配置如下

Image

CURL 访问参数如下

curl https://example.com/v1/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer ${HIGRESS_API_KEY}"
-d '{
"messages": [
{
"role": "user",
"content": "Who are you?"
}
],
"model": "gemini-2.0-flash"
}'
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
但是使用同一个 KEY,使用 CURL 调用官方接口是可以正常返回

curl "https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent"
-H 'Content-Type: application/json'
-H 'x-goog-api-key: GEMINI_API_KEY'
-d '{
"contents": [{
"parts":[{"text": "Who are you?"}]
}]
}'
{
"candidates": [
{
"content": {
"parts": [
{
"text": "I am a large language model, trained by Google.\n"
}
],
"role": "model"
},
"finishReason": "STOP",
"avgLogprobs": -3.197540354449302e-05
}
],
"usageMetadata": {
"promptTokenCount": 4,
"candidatesTokenCount": 12,
"totalTokenCount": 16,
"promptTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 4
}
],
"candidatesTokensDetails": [
{
"modality": "TEXT",
"tokenCount": 12
}
]
},
"modelVersion": "gemini-2.0-flash"
}
同样使用同一套 KEY, 我切换为 openai 兼容接口,也是可以正常调用

Image

curl https://example.com/v1/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer ${HIGRESS_API_KEY}"
-d '{
"messages": [
{
"role": "user",
"content": "Who are you?"
}
],
"model": "gemini-2.0-flash"
}'
{"choices":[{"finish_reason":"stop","index":0,"message":{"content":"I am a large language model, trained by Google.\n","role":"assistant"}}],"created":1746698000,"model":"gemini-2.0-flash","object":"chat.completion","usage":{"completion_tokens":12,"prompt_tokens":4,"total_tokens":16}}

我周末的时候自己复现下,到时候回复下

@CH3CHO CH3CHO added type/bug Something isn't working sig/wasm area/ai labels May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ai sig/wasm type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants