JeffreySu/WeiXinMPSDK · error · ErrorJsonResultException

微信请求发生错误(CommonApi.GetToken)!错误代码:

Error message

微信请求发生错误(CommonApi.GetToken)!错误代码:{0},说明:{1}

What it means

GetToken requests an access token from Weixin's OAuth endpoint; when Config.ThrownWhenJsonResultFaild is true and Weixin returns a non-success errcode, the library throws ErrorJsonResultException embedding the numeric error code and errmsg. It means Weixin itself rejected the token request.

Solutions

  1. Read result.errcode/errmsg in the exception (ErrorJsonResultException carries the JSON result) and look up the Weixin error code
  2. Verify appid and AppSecret against the Weixin admin console
  3. Add your server's outbound IP to the official account IP whitelist
  4. Optionally set Config.ThrownWhenJsonResultFaild = false if you prefer handling errcode manually

Example fix

// before
var token = CommonApi.GetToken(appId, wrongSecret);
// after
var token = CommonApi.GetToken(appId, secretFromConfig); // secret verified in Weixin admin console
Defensive patterns

Strategy: try-catch

Validate before calling

if (string.IsNullOrWhiteSpace(appid) || string.IsNullOrWhiteSpace(secret))
    throw new InvalidOperationException("appid/secret must be configured before GetToken");

Try / catch

try
{
    var token = CommonApi.GetToken(appid, secret);
}
catch (ErrorJsonResultException ex)
{
    logger.LogError("GetToken failed: {Code} {Msg}", (int)ex.JsonResult.errcode, ex.JsonResult.errmsg);
}

Prevention

When it happens

Trigger: Calling CommonApi.GetToken(appid, secret) where Weixin returns errcode such as 40001 (invalid secret) or 40013 (invalid appid), with Config.ThrownWhenJsonResultFaild enabled.

Common situations: Wrong or rotated AppSecret, appid/secret from a different environment, IP not on the Weixin official-account whitelist, or token quota issues.

Related errors


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

Appendix: source

Thrown at src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/CommonApi.cs:114

        /// <summary>
        /// 获取凭证接口
        /// </summary>
        /// <param name="grant_type">获取access_token填写client_credential</param>
        /// <param name="appid">第三方用户唯一凭证</param>
        /// <param name="secret">第三方用户唯一凭证密钥,既appsecret</param>
        /// <returns></returns>
        public static AccessTokenResult GetToken(string appid, string secret, string grant_type = "client_credential")
        {
            //注意:此方法不能再使用ApiHandlerWapper.TryCommonApi(),否则会循环
            var url = string.Format(Config.ApiMpHost + "/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
                                    grant_type.AsUrlData(), appid.AsUrlData(), secret.AsUrlData());

            AccessTokenResult result = Get.GetJson<AccessTokenResult>(CommonDI.CommonSP, url);//此处为最原始接口,不再使用重试获取的封装

            if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode.请求成功)
            {
                throw new ErrorJsonResultException(
                    string.Format("微信请求发生错误(CommonApi.GetToken)!错误代码:{0},说明:{1}",
                        (int)result.errcode, result.errmsg), null, result);
            }

            return result;
        }   
        
        /// <summary>
        /// 获取稳定版接口调用凭据
        /// </summary>
        /// <param name="grant_type">获取access_token填写client_credential</param>
        /// <param name="appid">账号唯一凭证,即 AppID,可在「微信公众平台 - 设置 - 开发设置」页中获得。(需要已经成为开发者,且帐号没有异常状态)</param>
        /// <param name="secret">帐号唯一凭证密钥,即 AppSecret,获取方式同 appid</param>
        /// <param name="force_refresh">默认使用 false。
        /// 1. force_refresh = false 时为普通调用模式,access_token 有效期内重复调用该接口不会更新 access_token;
        /// 2. 当force_refresh = true 时为强制刷新模式,会导致上次获取的 access_token 失效,并返回新的 access_token</param>
        /// <returns></returns>
        public static AccessTokenResult GetStableAccessToken(string appid, string secret, string grant_type = "client_credential",bool force_refresh=false)

View on GitHub (pinned to be573f6f94)