You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人调用Gemini API时遭遇401未授权错误的解决咨询

解决OpenClaw集成Discord机器人调用Gemini 2.5 Flash的401未授权问题

核心问题分析

日志明确返回UNAUTHENTICATED错误,说明当前配置的凭证无法通过Google API身份校验,结合你提到的凭证格式困惑,问题集中在凭证类型匹配和配置正确性上。

分步解决方案

1. 确认OpenClaw所需的凭证类型

Google Generative AI API支持两种凭证方式:

  • API密钥:格式以AIza开头(注意是AIza,不是你提到的Aiza),适用于服务器端无用户身份的调用场景。
  • OAuth 2.0令牌:用于需要用户授权的场景,比如访问用户私有数据。

OpenClaw集成Gemini时,大概率要求使用API密钥而非OAuth令牌。你遇到的系统提示可能是拼写误差,正确的Gemini API密钥前缀是AIza,AQ开头的凭证属于其他Google服务,不适用此处。

2. 重新生成并验证API密钥

  • 登录Google Cloud Console,进入你的项目。
  • 导航到「API和服务」→「凭据」。
  • 点击「创建凭据」→「API密钥」,生成新的密钥(确保前缀为AIza)。
  • 验证密钥权限:在「API和服务」→「已启用的API和服务」中,确认「Generative Language API」已启用;同时检查API密钥是否设置了IP限制或访问限制,测试阶段可暂时取消限制。

3. 检查OpenClaw的凭证配置

  • 找到OpenClaw中配置Google Gemini的入口,确保填入的是刚生成的AIza开头的API密钥,而非其他类型凭证。
  • 确认配置字段名正确(比如GOOGLE_API_KEY),避免填错位置。

4. 排查其他潜在问题

  • 确认你的Google Cloud项目未欠费,Gemini API配额未耗尽。
  • 检查机器人服务器网络环境,确保能正常访问Google API端点(无防火墙或代理拦截)。

日志信息参考

12:42:52 [discord] embedded run agent end: runId=0c5399eb-2146-42c0-bf47-a01045e47f21 isError=true model=gemini-2.5-flash provider=google error=LLM request failed. rawError=Google Generative AI API error (401): Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.googl…
12:42:52 [agent/embedded] auth profile failure state updated: runId=0c5399eb-2146-42c0-bf47-a01045e47f21 profile=sha256:a94403a4f9d0 provider=google reason=auth window=cooldown reused=false
12:42:52 [agent/embedded] embedded run failover decision: runId=0c5399eb-2146-42c0-bf47-a01045e47f21 stage=assistant decision=surface_error reason=auth from=google/gemini-2.5-flash profile=sha256:a94403a4f9d0 rawError=Google Generative AI API error (401): Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.googl…
12:42:52 [diagnostic] lane task error: lane=main durationMs=14670 error="FailoverError: Google Generative AI API error (401): 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. [code=UNAUTHENTICATED]"
12:42:52 [diagnostic] lane task error: lane=session:agent:main:discord:channel:1515861165627015241 durationMs=14675 error="FailoverError: Google Generative AI API error (401): 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. [code=UNAUTHENTICATED]"
12:42:52 [model-fallback/decision] model fallback decision: decision=candidate_failed requested=google/gemini-2.5-flash candidate=google/gemini-2.5-flash reason=auth next=none detail=Google Generative AI API error (401): 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. [code=UNAUTHENTICATED]
12:42:52 Embedded agent failed before reply: Google Generative AI API error (401): 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. [code=UNAUTHENTICATED]
12:42:55 [gateway] provider auth state re-warmed (auth-profile-failure) in 2702ms eventLoopMax=21.1ms

内容的提问来源于stack exchange,提问作者SunCe Khanh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 00:27:40