{"record":{"id":"acbbeec9f2dfcd4d","repo":"binarywang/WxJava","slug":"init-sdk-err-ret","errorCode":null,"errorMessage":"init sdk err ret {}","messagePattern":"init sdk err ret (.+?)","errorType":"exception","errorClass":"WxErrorException","httpStatus":null,"severity":"critical","filePath":"weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java","lineNumber":150,"sourceCode":"      if (s.contains(\"lib\")) {\n        osLib.add(s);\n      } else {\n        fileLib.add(s);\n      }\n    });\n    osLib.addAll(fileLib);\n\n    Finance.loadingLibraries(osLib, prefixPath);\n    long sdk = Finance.NewSdk();\n    // 因为会话存档单独有个secret,优先使用会话存档的secret\n    String msgAuditSecret = configStorage.getMsgAuditSecret();\n    if (StringUtils.isEmpty(msgAuditSecret)) {\n      msgAuditSecret = configStorage.getCorpSecret();\n    }\n    long ret = Finance.Init(sdk, configStorage.getCorpId(), msgAuditSecret);\n    if (ret != 0) {\n      Finance.DestroySdk(sdk);\n      throw new WxErrorException(\"init sdk err ret \" + ret);\n    }\n    return sdk;\n  }\n\n  @Override\n  public void closeThreadLocalSdk() {\n    Long sdk = threadLocalSdk.get();\n    // 先从 managedSdks 摘除，摘除成功才调 DestroySdk，防止与 closeAllSdks() 并发时 double-free\n    if (sdk != null && managedSdks.remove(sdk)) {\n      Finance.DestroySdk(sdk);\n      log.info(\"线程 [{}] 关闭会话存档SDK，sdk={}\", Thread.currentThread().getName(), sdk);\n    }\n    threadLocalSdk.remove();\n  }\n\n  @Override\n  public void closeAllSdks() {\n    // 逐一 remove 后再 Destroy，防止与 closeThreadLocalSdk() 并发时 double-free","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/binarywang/WxJava/blob/1c43293a3c2c9d7e91304b6d037fb017f680d0c6/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMsgAuditServiceImpl.java#L132-L168","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nWxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();\nconfig.setCorpId(\"corpId\");\nconfig.setCorpSecret(\"general_secret\"); // wrong secret for msg audit\nconfig.setMsgAuditLibPath(\"/opt/wecom/finance/libWeWorkFinanceSdk_Java.so\");\ncpService.getMsgAuditService().getChatDatas(0, 1000, null, null, 30); // init sdk err ret 10002\n\n// after\nconfig.setMsgAuditSecret(\"dedicated_msg_audit_secret\"); // from 会话内容存档 settings\ncpService.getMsgAuditService().getChatDatas(0, 1000, null, null, 30);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    cpService.getMsgAuditService().getChatDatas(seq, limit, proxy, passwd, timeout);\n} catch (WxErrorException e) {\n    String msg = e.getMessage(); // contains ret code\n    if (msg.contains(\"init sdk err ret\")) {\n        log.error(\"Finance.Init failed with ret code. Verify corpId and msgAuditSecret.\");\n    }\n    throw e;\n}","preventionTips":["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."],"tags":["weixin-cp","msg-audit","finance-sdk","native","config","authentication"],"backgroundTag":null,"analyzedSha":"1c43293a3c2c9d7e91304b6d037fb017f680d0c6","analyzedAt":"2026-08-14T02:29:11.060Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}