{"record":{"id":"095493296ad8d769","repo":"chenhg5/cc-connect","slug":"token-request-failed-w","errorCode":null,"errorMessage":"token request failed: %w","messagePattern":"token request failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/qqbot/qqbot.go","lineNumber":576,"sourceCode":"\t\tmessageType: \"c2c\",\n\t\tuserOpenID:  parts[1],\n\t\tsessionKey:  sessionKey,\n\t}, nil\n}\n\n// ---------------------------------------------------------------------------\n// OAuth2 Token Management\n// ---------------------------------------------------------------------------\n\nfunc (p *Platform) refreshToken() error {\n\tbody, _ := json.Marshal(map[string]string{\n\t\t\"appId\":        p.appID,\n\t\t\"clientSecret\": p.appSecret,\n\t})\n\n\tresp, err := core.HTTPClient.Post(tokenURL, \"application/json\", bytes.NewReader(body))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"token request failed: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\traw, _ := io.ReadAll(resp.Body)\n\t\treturn fmt.Errorf(\"token request returned %d: %s\", resp.StatusCode, raw)\n\t}\n\n\tvar result struct {\n\t\tAccessToken string `json:\"access_token\"`\n\t\tExpiresIn   string `json:\"expires_in\"`\n\t}\n\tif err := json.NewDecoder(resp.Body).Decode(&result); err != nil {\n\t\treturn fmt.Errorf(\"token response decode: %w\", err)\n\t}\n\tif result.AccessToken == \"\" {\n\t\treturn fmt.Errorf(\"empty access_token in response\")\n\t}","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/qqbot/qqbot.go#L558-L594","documentation":"refreshToken posts appId/clientSecret to QQ's OAuth token endpoint; this error wraps a transport-level failure of that POST (platform/qqbot/qqbot.go:576). It means the HTTP request itself never completed — DNS, TCP, TLS, or proxy failure — not an HTTP error status from the server.","triggerScenarios":"Any code path calling refreshToken (startup via connectGateway, getAccessToken on expiry, 401-retry in apiRequest) when core.HTTPClient.Post returns an error: network down, DNS failure, TLS error, request timeout, proxy misconfiguration.","commonSituations":"Host without internet access; corporate firewall blocking api.sgroup.qq.com; bad HTTP_PROXY env vars; transient network blips during token refresh after the 2-hour token lifetime.","solutions":["Check the wrapped cause (%w) to identify DNS vs TCP vs TLS vs timeout.","Verify outbound HTTPS connectivity from the host to the QQ Bot token endpoint.","Clear/fix HTTP_PROXY/HTTPS_PROXY environment variables if a proxy is misconfigured.","Add retry with backoff around platform start, or ensure the daemon restarts on repeated failures."],"exampleFix":"// before\nif err := platform.Start(ctx); err != nil { panic(err) }\n// after\nif err := platform.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"token request failed\") {\n        slog.Warn(\"qqbot token fetch failed, retrying\", \"err\", err)\n        time.Sleep(5 * time.Second)\n        err = platform.Start(ctx)\n    }\n}","handlingStrategy":"retry","validationCode":"// before starting the platform:\nconn, err := net.DialTimeout(\"tcp\", \"api.sgroup.qq.com:443\", 5*time.Second)\nif err != nil { return fmt.Errorf(\"QQ Bot API unreachable: %w\", err) }\nconn.Close()","typeGuard":null,"tryCatchPattern":"if err := platform.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"token request failed\") {\n        // network-level failure: retry with backoff\n        backoff := 5 * time.Second\n        for i := 0; i < 3; i++ {\n            time.Sleep(backoff); backoff *= 2\n            if err = platform.Start(ctx); err == nil { break }\n        }\n    }\n}","preventionTips":["Ensure the host has stable outbound HTTPS (test with curl to the QQ endpoint).","Audit HTTP_PROXY/HTTPS_PROXY settings on the deployment host.","Run cc-connect under systemd/launchd with restart-on-failure so transient network blips self-heal.","Monitor connectivity and alert before token expiry windows."],"tags":["network","oauth","qqbot","token"],"backgroundTag":"network-request-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}