{"record":{"id":"8a224a684e5d14e0","repo":"chenhg5/cc-connect","slug":"get-access-token-w-8a224a","errorCode":null,"errorMessage":"get access token: %w","messagePattern":"get access token: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/dingtalk/dingtalk.go","lineNumber":677,"sourceCode":"\n\tdata, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, \"\", fmt.Errorf(\"read response: %w\", err)\n\t}\n\n\t// Determine MIME type from Content-Type header\n\tmimeType := resp.Header.Get(\"Content-Type\")\n\tif mimeType == \"\" {\n\t\tmimeType = \"audio/amr\" // Default to AMR if not specified\n\t}\n\n\treturn data, mimeType, nil\n}\n\nfunc (p *Platform) getDownloadURL(downloadCode string) (string, error) {\n\ttoken, err := p.getAccessToken()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"get access token: %w\", err)\n\t}\n\n\treqBody := map[string]string{\n\t\t\"downloadCode\": downloadCode,\n\t\t\"robotCode\":    p.robotCode,\n\t}\n\tbodyBytes, err := json.Marshal(reqBody)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"marshal request: %w\", err)\n\t}\n\n\tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\n\tdefer cancel()\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost,\n\t\t\"https://api.dingtalk.com/v1.0/robot/messageFiles/download\",\n\t\tbytes.NewReader(bodyBytes))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"create request: %w\", err)","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/dingtalk/dingtalk.go#L659-L695","documentation":"getDownloadURL wraps failures from getAccessToken. Before calling the messageFiles/download API, the platform must fetch an OAuth access token; this error means token acquisition failed (bad credentials, network, or token API error).","triggerScenarios":"Called from handleImageMessage, handleFileMessage, or downloadAudio when p.getAccessToken() returned an error — typically invalid client_id/client_secret or a failed call to the DingTalk token endpoint.","commonSituations":"Wrong or rotated AppKey/AppSecret; DingTalk app disabled or permissions revoked; no outbound access to DingTalk auth endpoint; token endpoint rate limiting after aggressive polling.","solutions":["Verify client_id/client_secret are the correct, currently valid AppKey/AppSecret for your DingTalk app.","Check the wrapped cause: 4xx indicates bad credentials, network errors indicate connectivity.","Confirm the app is enabled and has the robot/media download permissions granted in the DingTalk developer console.","If rate-limited, back off and cache the token until near expiry."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func validateDingtalkCredentials(clientID, clientSecret string) error {\n    req, _ := http.NewRequest(http.MethodPost, \"https://api.dingtalk.com/v1.0/oauth2/accessToken\",\n        strings.NewReader(fmt.Sprintf(`{\"appKey\":%q,\"appSecret\":%q}`, clientID, clientSecret)))\n    req.Header.Set(\"Content-Type\", \"application/json\")\n    resp, err := http.DefaultClient.Do(req)\n    if err != nil { return err }\n    defer resp.Body.Close()\n    if resp.StatusCode != http.StatusOK {\n        b, _ := io.ReadAll(resp.Body)\n        return fmt.Errorf(\"token check failed: %d %s\", resp.StatusCode, b)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"get access token\") {\n    // credentials/auth problem: alert ops, do not retry blindly\n    log.Error(\"dingtalk auth misconfigured\", \"err\", err)\n}","preventionTips":["Validate AppKey/AppSecret once at startup with a token probe","Rotate secrets via config reload, not in-place edits","Cache tokens until near expiry to avoid rate limits"],"tags":["dingtalk","oauth","token","api"],"backgroundTag":"oauth-token-exchange-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"}