chenhg5/cc-connect · error

wecom: get token failed: %d %s

Error message

wecom: get token failed: %d %s

What it means

WeCom API rejection of the token request: the gettoken response parsed but errcode was non-zero. Classic codes: 40013 invalid corpid, 40125 invalid corpsecret, 40029 bad appid/secret pair. This is a credential/configuration error surfaced at token-fetch time and affects every subsequent API call.

Source

Thrown at platform/wecom/wecom.go:686

	})

	resp, err := p.apiClient.Get(apiURL)
	if err != nil {
		return "", fmt.Errorf("wecom: request access_token: %w", err)
	}
	defer resp.Body.Close()

	var result struct {
		ErrCode     int    `json:"errcode"`
		ErrMsg      string `json:"errmsg"`
		AccessToken string `json:"access_token"`
		ExpiresIn   int    `json:"expires_in"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return "", fmt.Errorf("wecom: decode token response: %w", err)
	}
	if result.ErrCode != 0 {
		return "", fmt.Errorf("wecom: get token failed: %d %s", result.ErrCode, result.ErrMsg)
	}

	// Compute the cache window from expires_in with a 60-second safety
	// margin. When the server omits or zeroes the field, fall back to
	// WeCom's documented 7200s default; without this, the raw value would
	// land at -60 and the cache would be stale on the very next call,
	// turning every outbound API request into a fresh /gettoken round-trip.
	expires := result.ExpiresIn
	if expires <= 0 {
		slog.Warn("wecom: missing/invalid expires_in in token response, defaulting to 7200s", "got", result.ExpiresIn)
		expires = 7200
	}
	if expires > 60 {
		expires -= 60
	}
	p.tokenCache.token = result.AccessToken
	p.tokenCache.expiresAt = time.Now().Add(time.Duration(expires) * time.Second)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify corp_id and corp_secret in config.toml against the WeCom admin console
  2. Confirm the secret belongs to the same app/agent as agentid
  3. Redact the secret from any logs (core.RedactToken)
  4. Do not hot-retry invalid-credential errors; fail fast with a clear i18n message
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at platform/wecom/wecom.go:686 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/f8fcc52326e9df43. Report an issue: GitHub.