{"record":{"id":"b203f1f958c52fb1","repo":"JeffreySu/WeiXinMPSDK","slug":"commonapi-getstableaccesstokenasync-0-1","errorCode":null,"errorMessage":"微信请求发生错误（CommonApi.GetStableAccessTokenAsync）！错误代码：{0}，说明：{1}","messagePattern":"微信请求发生错误（CommonApi\\.GetStableAccessTokenAsync）！错误代码：(.+?)，说明：(.+?)","errorType":"exception","errorClass":"ErrorJsonResultException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/CommonApi.cs","lineNumber":356,"sourceCode":"        /// <param name=\"force_refresh\">默认使用 false。\n        /// 1. force_refresh = false 时为普通调用模式，access_token 有效期内重复调用该接口不会更新 access_token；\n        /// 2. 当force_refresh = true 时为强制刷新模式，会导致上次获取的 access_token 失效，并返回新的 access_token</param>\n        /// <returns></returns>\n        public static async Task<AccessTokenResult> GetStableAccessTokenAsync(string appid, string secret, string grant_type = \"client_credential\",bool force_refresh=false)\n        {\n            var url = Config.ApiMpHost + \"/cgi-bin/stable_token\";\n            var data = new\n            {\n                grant_type= \"client_credential\",\n                appid= appid,\n                secret= secret,\n                force_refresh= force_refresh\n            };\n\n            AccessTokenResult result = await CommonJsonSend.SendAsync<AccessTokenResult>(null, url, data, CommonJsonSendType.POST);\n            if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode.请求成功)\n            {\n                throw new ErrorJsonResultException(string.Format(\"微信请求发生错误（CommonApi.GetStableAccessTokenAsync）！错误代码：{0}，说明：{1}\",\n                        (int)result.errcode, result.errmsg), null, result);\n            }\n\n            return result;\n        }\n\n        //已经迁移到 UserApi 下\n        ///// <summary>\n        ///// 【异步方法】用户信息接口\n        ///// </summary>\n        ///// <param name=\"accessTokenOrAppId\">AccessToken或AppId（推荐使用AppId，需要先注册）</param>\n        ///// <param name=\"openId\"></param>\n        ///// <returns></returns>\n        //public static async Task<WeixinUserInfoResult> GetUserInfoAsync(string accessTokenOrAppId, string openId)\n        //{\n        //    return await ApiHandlerWapper.TryCommonApiAsync(async accessToken =>\n        //   {\n        //       var url = string.Format(Config.ApiMpHost + \"/cgi-bin/user/info?access_token={0}&openid={1}\",","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/CommonApi.cs#L338-L374","documentation":"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.","triggerScenarios":"Calling GetStableAccessTokenAsync with invalid credentials, an IP not whitelisted, or excessive force_refresh causing rate-limit errcodes.","commonSituations":"Async migrations using stale secrets, CI/CD environments with new outbound IPs, high-traffic services hitting token refresh limits.","solutions":["Inspect the errcode in the exception and resolve the named Weixin issue","Validate appid/secret configuration","Whitelist the deployment environment's IP","Cache tokens and only call force_refresh when necessary"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (string.IsNullOrWhiteSpace(appid) || string.IsNullOrWhiteSpace(secret))\n    throw new InvalidOperationException(\"appid/secret required before GetStableAccessTokenAsync\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var token = await CommonApi.GetStableAccessTokenAsync(appid, secret, forceRefresh);\n}\ncatch (ErrorJsonResultException ex)\n{\n    logger.LogError(\"GetStableAccessTokenAsync failed: {Code} {Msg}\", (int)ex.JsonResult.errcode, ex.JsonResult.errmsg);\n}","preventionTips":["Reuse tokens across requests via a token container","Limit force_refresh calls","Validate credentials at startup with a smoke test","Alert on repeated token endpoint errors"],"tags":["weixin","api-error","async","access-token"],"backgroundTag":"api-error-response","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}