agent executor error: request failed: Post "https://cloudcode-pa.googleapis.com/...

2026年04月23日 22:06 processing

错误信息

Trajectory ID: c75aa7de-11b5-484a-b1cc-b80eed82c14d Error: agent executor error: request failed: Post "https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse": oauth2: "unauthorized_client" "Unauthorized": request failed: Post "https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse": oauth2: "unauthorized_client" "Unauthorized" (1) attached stack trace -- stack trace: | google3/third_party/gemini_coder/framework/executor/agentexecutor/agentexecutor.(*AgentExecutor).Run | third_party/gemini_coder/framework/executor/agentexecutor/agentexecutor.go:339 | [...repeated from below...] Wraps: (2) agent executor error Wraps: (3) tags: map[stream_receive_count:0 streaming_duration:0s] Wraps: (4) attached stack trace -- stack trace: | google3/third_party/gemini_coder/framework/generator/generator.(*streamResponseHandler).processStream | third_party/gemini_coder/framework/generator/stream_handler.go:329 | google3/third_party/gemini_coder/framework/generator/generator.(*PlannerGenerator).attemptGenerate | third_party/gemini_coder/framework/generator/planner_generator.go:451 | google3/third_party/gemini_coder/framework/generator/generator.(*PlannerGenerator).generateWithAPIRetry | third_party/gemini_coder/framework/generator/planner_generator.go:286 | google3/third_party/gemini_coder/framework/generator/generator.(*PlannerGenerator).generateWithModelOutputRetry | third_party/gemini_coder/framework/generator/planner_generator.go:148 | google3/third_party/gemini_coder/framework/generator/generator.(*PlannerGenerator).Generate | third_party/gemini_coder/framework/generator/planner_generator.go:91 | google3/third_party/gemini_coder/framework/executor/executor.(*Executor).executeLoop | third_party/gemini_coder/framework/executor/executor.go:369 | google3/third_party/gemini_coder/framework/executor/executor.(*Executor).Execute | third_party/gemini_coder/framework/executor/executor.go:288 | google3/third_party/gemini_coder/framework/executor/agentexecutor/agentexecutor.(*AgentExecutor).Run | third_party/gemini_coder/framework/executor/agentexecutor/agentexecutor.go:305 | google3/third_party/jetski/cortex/cortex.(*CascadeManager).executeHelper.func1 | third_party/jetski/cortex/cascade_manager.go:1732 | google3/third_party/jetski/cortex/utils/background.(*Pool).Go.func2 | third_party/jetski/cortex/utils/background.go:121 | google3/go/context/detach.Go.func1 | go/context/detach.go:247 | runtime.goexit | third_party/go/gc/src/runtime/asm_amd64.s:1774 Wraps: (5) request failed: Post "https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse": oauth2: "unauthorized_client" "Unauthorized" Wraps: (6) request failed Wraps: (7) Post "https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse" Wraps: (8) oauth2: "unauthorized_client" "Unauthorized" Error types: (1) *withstack.withStack (2) *errutil.withPrefix (3) *go_utils.withTags (4) *withstack.withStack (5) *errutil.withPrefix (6) *fmt.wrapError (7) *url.Error (8) *oauth2.RetrieveError

AI 解决方案

报错分析报告

1.错误翻译


原文:oauth2: "unauthorized_client" "Unauthorized"
译文:oauth2:“未授权客户端”“未授权”

2.原因分析


  • 根本原因:OAuth2客户端未获授权,无法访问https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent接口。

  • 发生位置:调用Google Cloud Code的流式生成API时,认证凭证(客户端ID/密钥、访问令牌)无效或权限不足。

  • 上下文:错误嵌套在AgentExecutor.RunstreamResponseHandler.processStream流程中,最终因API请求被拒绝触发。
  • 3.解决方案


    // 修复步骤(以Go为例):
    // 1. 检查OAuth2配置是否正确(客户端ID、密钥、 scopes)
    // 2. 确保访问令牌未过期,若过期则刷新或重新授权
    // 3. 验证API权限(需开启Cloud Code API并授予客户端访问权)
    
    import (
        "golang.org/x/oauth2"
        "google.golang.org/api/option"
    )
    
    func createAuthorizedClient(config *oauth2.Config, token *oauth2.Token) (*http.Client, error) {
        // 检查令牌有效性
        if !token.Valid() {
            var err error
            token, err = config.TokenSource(context.Background(), token).Token()
            if err != nil {
                return nil, fmt.Errorf("刷新令牌失败: %w", err)
            }
        }
        return config.Client(context.Background(), token), nil
    }
    
    // 使用示例:替换原HTTP客户端为授权后的客户端
    client, err := createAuthorizedClient(yourOAuthConfig, yourStoredToken)
    if err != nil {
        log.Fatalf("创建授权客户端失败: %v", err)
    }
    resp, err := client.Post("https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse", ...)
    

    4. 预防措施


  • 最佳实践1:使用oauth2.TokenSource自动管理令牌刷新,避免手动处理过期问题。

  • 最佳实践2:通过Google Cloud Console的「API和服务」→「凭据」页面,定期验证OAuth2客户端的权限范围。

  • 推荐工具:用curl或Postman预测试API请求的Authorization头(如Bearer <token>),快速定位认证问题。