Skip to content

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

@daixijun

Description

@daixijun

使用 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}}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions