JeffreySu/WeiXinMPSDK · error · ArgumentException

请求未提供 sig 时必须提供收银台 API 调用密钥。

Error message

请求未提供 sig 时必须提供收银台 API 调用密钥。

What it means

PrepareRequest throws this ArgumentException when the outgoing PayTool (企业微信收银台) request does not carry a sig parameter and no payToolApiSecret was supplied. The library needs the cashier API secret to compute the signature itself; without sig or a secret the request cannot be authenticated by WeChat Work. It is thrown before the request is sent.

Solutions

  1. Pass a non-empty payToolApiSecret to PrepareRequest so the library can generate the sig.
  2. If you sign requests yourself, set request.sig to a valid Base64 signature before calling.
  3. Verify the secret value is actually loaded from configuration (not null/empty) before invoking the API.

Example fix

// before
PayToolApi.PrepareRequest(request, payToolApiSecret: "");
// after
if (string.IsNullOrWhiteSpace(secret)) throw new InvalidOperationException("配置缺少收银台 API 密钥");
PayToolApi.PrepareRequest(request, payToolApiSecret: secret);
Defensive patterns

Strategy: validation

Validate before calling

bool canCall = !string.IsNullOrWhiteSpace(payToolApiSecret) || !string.IsNullOrEmpty(request.sig);

Try / catch

try { PrepareRequest(request, secret); } catch (ArgumentException ex) when (ex.ParamName == nameof(payToolApiSecret)) { logger.LogError(ex, "缺少收银台 API 密钥"); throw new ConfigurationException("收银台 API 密钥未配置"); }

Prevention

When it happens

Trigger: Calling a PayTool API through PrepareRequest with request.sig left empty and passing payToolApiSecret as null, empty string, or whitespace (e.g. reading the secret from an unpopulated config field).

Common situations: Developers omit the cashier API secret in appsettings/environment config, or pass an empty string because the secret was never issued/configured for their WeChat Work merchant, or rely on sig-in-request mode but forgot to actually sign the request.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/63df3493956901da. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/PayTool/PayToolSignatureHelper.cs:56

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!string.IsNullOrEmpty(request.sig))
            {
                if (string.IsNullOrEmpty(request.nonce_str) || request.ts <= 0)
                {
                    throw new ArgumentException("预签名请求必须同时提供 nonce_str 和 ts。",
                        nameof(request));
                }

                return;
            }

            if (string.IsNullOrEmpty(payToolApiSecret))
            {
                throw new ArgumentException("请求未提供 sig 时必须提供收银台 API 调用密钥。",
                    nameof(payToolApiSecret));
            }

            if (string.IsNullOrEmpty(request.nonce_str))
            {
                request.nonce_str = Guid.NewGuid().ToString("N");
            }

            if (request.ts <= 0)
            {
                request.ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            }

            request.sig = CreateSignature(request, payToolApiSecret);
        }

        /// <summary>
        /// 按企业微信规则递归展开非空叶子参数、按完整 key=value 字符串升序排列,

View on GitHub (pinned to be573f6f94)