binarywang/WxJava · critical · WxErrorException
init sdk err ret {}
Error message
init sdk err ret {} What it means
Thrown as WxErrorException (checked) in createSdk() when the native Finance.Init() function returns a non-zero ret code after loading libraries and creating the SDK handle. Finance.Init authenticates with the corpId and msgAuditSecret (or falls back to corpSecret); a non-zero return means authentication or initialization failed at the native level. The SDK handle is properly destroyed before throwing.
Source
Thrown at weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java:150
if (s.contains("lib")) {
osLib.add(s);
} else {
fileLib.add(s);
}
});
osLib.addAll(fileLib);
Finance.loadingLibraries(osLib, prefixPath);
long sdk = Finance.NewSdk();
// 因为会话存档单独有个secret,优先使用会话存档的secret
String msgAuditSecret = configStorage.getMsgAuditSecret();
if (StringUtils.isEmpty(msgAuditSecret)) {
msgAuditSecret = configStorage.getCorpSecret();
}
long ret = Finance.Init(sdk, configStorage.getCorpId(), msgAuditSecret);
if (ret != 0) {
Finance.DestroySdk(sdk);
throw new WxErrorException("init sdk err ret " + ret);
}
return sdk;
}
@Override
public void closeThreadLocalSdk() {
Long sdk = threadLocalSdk.get();
// 先从 managedSdks 摘除,摘除成功才调 DestroySdk,防止与 closeAllSdks() 并发时 double-free
if (sdk != null && managedSdks.remove(sdk)) {
Finance.DestroySdk(sdk);
log.info("线程 [{}] 关闭会话存档SDK,sdk={}", Thread.currentThread().getName(), sdk);
}
threadLocalSdk.remove();
}
@Override
public void closeAllSdks() {
// 逐一 remove 后再 Destroy,防止与 closeThreadLocalSdk() 并发时 double-freeView on GitHub (pinned to 1c43293a3c)
Solutions
- Inspect the ret code in the exception message (e.g., 10002 = secret error) to pinpoint the cause.
- Set the dedicated conversation archive secret: config.setMsgAuditSecret("your_msg_audit_secret") from WeCom admin > 管理工具 > 会话内容存档.
- Verify corpId matches the enterprise exactly (no trailing spaces or wrong casing).
- Ensure the conversation archive feature is enabled for the enterprise and the API permissions are granted.
Example fix
// before
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setCorpId("corpId");
config.setCorpSecret("general_secret"); // wrong secret for msg audit
config.setMsgAuditLibPath("/opt/wecom/finance/libWeWorkFinanceSdk_Java.so");
cpService.getMsgAuditService().getChatDatas(0, 1000, null, null, 30); // init sdk err ret 10002
// after
config.setMsgAuditSecret("dedicated_msg_audit_secret"); // from 会话内容存档 settings
cpService.getMsgAuditService().getChatDatas(0, 1000, null, null, 30); Defensive patterns
Strategy: try-catch
Try / catch
try {
cpService.getMsgAuditService().getChatDatas(seq, limit, proxy, passwd, timeout);
} catch (WxErrorException e) {
String msg = e.getMessage(); // contains ret code
if (msg.contains("init sdk err ret")) {
log.error("Finance.Init failed with ret code. Verify corpId and msgAuditSecret.");
}
throw e;
} Prevention
- Use the dedicated conversation archive secret (msgAuditSecret), not the general corpSecret.
- Verify corpId and secret are correct and the conversation archive feature is enabled.
- Test Finance.Init in a staging environment before production deployment.
When it happens
Trigger: Calling createSdk() (indirectly via getChatDatas or getDecryptData) where Finance.Init fails. The most common cause is an incorrect msgAuditSecret or corpId, but it can also indicate expired credentials or a revoked API permission for conversation archive access.
Common situations: Developer uses the general corpSecret instead of the dedicated conversation archive secret (msgAuditSecret). Or the secret was regenerated in the WeCom admin console but the application config was not updated. The corpId may also be mistyped.
Related errors
- getchatdata err ret {}
- 请配置会话存档sdk文件的路径,不要配错了!!
- 请仔细配置会话存档文件路径!!
- 请配置会话存档私钥【msgAuditPriKey】
- 线程 [{}] 获取会话存档SDK失败,请检查是否已调用 closeAllSdks()
AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14).
Data as JSON: /api/errors/acbbeec9f2dfcd4d.
Report an issue: GitHub.