JeffreySu/WeiXinMPSDK · error · ErrorJsonResultException

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

Error message

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

What it means

GetStableAccessTokenAsync throws ErrorJsonResultException when the stable-token endpoint returns a non-success errcode and Config.ThrownWhenJsonResultFaild is true. The exception carries the Weixin error code and description.

Solutions

  1. Inspect the errcode in the exception and resolve the named Weixin issue
  2. Validate appid/secret configuration
  3. Whitelist the deployment environment's IP
  4. Cache tokens and only call force_refresh when necessary
Defensive patterns

Strategy: try-catch

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Calling GetStableAccessTokenAsync with invalid credentials, an IP not whitelisted, or excessive force_refresh causing rate-limit errcodes.

Common situations: Async migrations using stale secrets, CI/CD environments with new outbound IPs, high-traffic services hitting token refresh limits.

Related errors


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

Appendix: source

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

        /// <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 async Task<AccessTokenResult> GetStableAccessTokenAsync(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 = await CommonJsonSend.SendAsync<AccessTokenResult>(null, url, data, CommonJsonSendType.POST);
            if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode.请求成功)
            {
                throw new ErrorJsonResultException(string.Format("微信请求发生错误(CommonApi.GetStableAccessTokenAsync)!错误代码:{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 async Task<WeixinUserInfoResult> GetUserInfoAsync(string accessTokenOrAppId, string openId)
        //{
        //    return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>
        //   {
        //       var url = string.Format(Config.ApiMpHost + "/cgi-bin/user/info?access_token={0}&openid={1}",

View on GitHub (pinned to be573f6f94)