JeffreySu/WeiXinMPSDK · error · WeixinWorkException
微信请求发生错误(CommonApi.GetToken)!错误代码:
Error message
微信请求发生错误(CommonApi.GetToken)!错误代码:{0},说明:{1} What it means
CommonApi.GetToken throws WeixinWorkException with message '微信请求发生错误(CommonApi.GetToken)!错误代码:{0},说明:{1}' when the gettoken endpoint returns an errcode other than 请求成功 (0) and Config.ThrownWhenJsonResultFaild is true. This is the library's standard 'WeChat API rejected the request' wrapper for token acquisition, embedding the numeric error code and errmsg from the server.
Solutions
- Read the embedded errcode/errmsg: verify corpId and corpSecret against the WeChat Work admin console.
- Add the outbound server IP to the trusted IP whitelist of the app (errcode 60020).
- Rotate/reissue the secret if it was reset, and update configuration; use Config.SenparcWeixinSetting for centralized credentials.
- Set Config.ThrownWhenJsonResultFaild=false only if you prefer to inspect errcode manually instead of exceptions (retry-safe).
Example fix
// before
var token = CommonApi.GetToken("wrong-corp", "bad-secret"); // throws
// after
try
{
var token = CommonApi.GetToken(corpId, corpSecret);
}
catch (WeixinWorkException ex)
{
// inspect ex.Message for errcode (e.g. 40013 invalid corpid, 60020 IP not whitelisted)
}
Defensive patterns
Strategy: try-catch
Validate before calling
// before calling
if (string.IsNullOrWhiteSpace(corpId) || string.IsNullOrWhiteSpace(corpSecret))
throw new InvalidOperationException("corpId/corpSecret not configured"); Try / catch
try { var token = CommonApi.GetToken(corpId, corpSecret); }
catch (WeixinWorkException ex)
{
// parse errcode from ex.Message: 40013 bad corpid, 40001 bad secret, 60020 IP not whitelisted
} Prevention
- Store credentials in SenparcWeixinSetting config section, not hardcoded
- Whitelist server egress IPs in the WeChat Work admin console
- Rotate secrets via a documented process and update config immediately
- Cache tokens via the library's container instead of fetching per request
When it happens
Trigger: CommonApi.GetToken(corpId, corpSecret) where the WeChat Work server returns a non-zero errcode: wrong corpId/corpSecret, IP not in the app's trusted whitelist, secret deleted/rotated, or network returning an API-level failure payload.
Common situations: Config mistakes: copied wrong secret, swapped corpId/secret; server IP changed and not whitelisted in WeChat Work admin (errcode 60020); secret regenerated after a security rotation while configs still hold the old value; ThrownWhenJsonResultFaild default-on surprising callers.
Related errors
- {errmsg}
- 微信请求发生错误(CommonApi.GetToken)!错误代码:
- 微信请求发生错误(CommonApi.GetStableAccessToken)!错误代码:
- 微信请求发生错误(CommonApi.GetStableAccessTokenAsync)!错误代码:
- ArgumentNullException (response is null)
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/990cd3c462566aef.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/CommonAPIs/CommonApi.cs:98
每ip调用单个cgi/api不可超过2000次/分,60000次/小时
每ip获取AccessToken不可超过300次/小时
发消息频率
每企业不可超过200次/分钟;不可超过帐号上限数*30人次/天
创建帐号频率
每企业创建帐号数不可超过帐号上限数*3/月
*/
#endregion
var url = string.Format(Config.ApiWorkHost + "/cgi-bin/gettoken?corpid={0}&corpsecret={1}", corpId.AsUrlData(), corpSecret.AsUrlData());
var result = CommonJsonSend.Send<AccessTokenResult>(null, url, null, CommonJsonSendType.GET);
if (Config.ThrownWhenJsonResultFaild && result.errcode != ReturnCode_Work.请求成功)
{
throw new WeixinWorkException(
string.Format("微信请求发生错误(CommonApi.GetToken)!错误代码:{0},说明:{1}",
(int)result.errcode, result.errmsg), null);
}
return result;
}
/// <summary>
/// 获取微信服务器的ip段
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
public static GetCallBackIpResult GetCallBackIp(string accessToken)
{
var url = string.Format(Config.ApiWorkHost + "/cgi-bin/getcallbackip?access_token={0}", accessToken.AsUrlData());
return CommonJsonSend.Send<GetCallBackIpResult>(null, url, null, CommonJsonSendType.GET);
}View on GitHub (pinned to be573f6f94)