JeffreySu/WeiXinMPSDK · error · ErrorJsonResultException

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

Error message

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

What it means

GetStableAccessToken posts to Weixin's stable-token endpoint; if the response errcode is not 请求成功 and Config.ThrownWhenJsonResultFaild is true, an ErrorJsonResultException is thrown with the code and message. Unlike the classic token endpoint, this one supports force_refresh and returns the same error semantics.

Solutions

  1. Inspect the errcode/errmsg carried by ErrorJsonResultException and fix the credential or request issue it names
  2. Re-verify appid/secret in the Weixin console
  3. Whitelist your server IP
  4. Tune force_refresh usage to avoid unnecessary refreshes and rate limits
Defensive patterns

Strategy: try-catch

Validate before calling

if (string.IsNullOrWhiteSpace(appid) || string.IsNullOrWhiteSpace(secret))
    throw new InvalidOperationException("appid/secret required for GetStableAccessToken");

Try / catch

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

Prevention

When it happens

Trigger: Calling GetStableAccessToken with invalid appid/secret, force_refresh misuse, or Weixin-side rejection (e.g. 40001 invalid credential) while ThrownWhenJsonResultFaild is enabled.

Common situations: Migrating from GetToken to the stable-token API with credentials that worked elsewhere but are stale here; IP whitelist restrictions; refresh storms triggering rate limits.

Related errors


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

Appendix: source

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

        /// <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)
        {
            var url = Config.ApiMpHost + "/cgi-bin/stable_token";
            var data = new
            {
                grant_type= "client_credential",
                appid= appid,
                secret= secret,
                force_refresh= force_refresh
            };
            AccessTokenResult result = CommonJsonSend.Send<AccessTokenResult>(null, url, data, CommonJsonSendType.POST);
            if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode.请求成功)
            {
                throw new ErrorJsonResultException(
                    string.Format("微信请求发生错误(CommonApi.GetStableAccessToken)!错误代码:{0},说明:{1}",
                        (int)result.errcode, result.errmsg), null, result);
            }

            return result;
        }

        //已经迁移到 UserApi 下
        ///// <summary>
        ///// 用户信息接口
        ///// </summary>
        ///// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
        ///// <param name="openId"></param>
        ///// <returns></returns>
        //public static WeixinUserInfoResult GetUserInfo(string accessTokenOrAppId, string openId)
        //{
        //    return ApiHandlerWapper.TryCommonApi(accessToken =>
        //    {

View on GitHub (pinned to be573f6f94)