{"record":{"id":"9a77161dde83bf7f","repo":"JeffreySu/WeiXinMPSDK","slug":"commonapi-getstableaccesstoken-0-1","errorCode":null,"errorMessage":"微信请求发生错误（CommonApi.GetStableAccessToken）！错误代码：{0}，说明：{1}","messagePattern":"微信请求发生错误（CommonApi\\.GetStableAccessToken）！错误代码：(.+?)，说明：(.+?)","errorType":"exception","errorClass":"ErrorJsonResultException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/CommonApi.cs","lineNumber":145,"sourceCode":"        /// <param name=\"secret\">帐号唯一凭证密钥，即 AppSecret，获取方式同 appid</param>\n        /// <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 AccessTokenResult GetStableAccessToken(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            AccessTokenResult result = CommonJsonSend.Send<AccessTokenResult>(null, url, data, CommonJsonSendType.POST);\n            if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode.请求成功)\n            {\n                throw new ErrorJsonResultException(\n                    string.Format(\"微信请求发生错误（CommonApi.GetStableAccessToken）！错误代码：{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 WeixinUserInfoResult GetUserInfo(string accessTokenOrAppId, string openId)\n        //{\n        //    return ApiHandlerWapper.TryCommonApi(accessToken =>\n        //    {","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/CommonApi.cs#L127-L163","documentation":"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.","triggerScenarios":"Calling GetStableAccessToken with invalid appid/secret, force_refresh misuse, or Weixin-side rejection (e.g. 40001 invalid credential) while ThrownWhenJsonResultFaild is enabled.","commonSituations":"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.","solutions":["Inspect the errcode/errmsg carried by ErrorJsonResultException and fix the credential or request issue it names","Re-verify appid/secret in the Weixin console","Whitelist your server IP","Tune force_refresh usage to avoid unnecessary refreshes and rate limits"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (string.IsNullOrWhiteSpace(appid) || string.IsNullOrWhiteSpace(secret))\n    throw new InvalidOperationException(\"appid/secret required for GetStableAccessToken\");","typeGuard":null,"tryCatchPattern":"try\n{\n    var token = CommonApi.GetStableAccessToken(appid, secret, forceRefresh);\n}\ncatch (ErrorJsonResultException ex)\n{\n    logger.LogError(\"Stable token failed: {Code} {Msg}\", (int)ex.JsonResult.errcode, ex.JsonResult.errmsg);\n}","preventionTips":["Verify credentials before deploying","Use force_refresh sparingly to avoid rate limits","Cache the stable token until expiry","Monitor errcode 40001 as an indicator of rotated secrets"],"tags":["weixin","api-error","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"}