{"record":{"id":"33a9abf78d9d950e","repo":"justauth/JustAuth","slug":"5016","errorCode":"5016","errorMessage":"Illegal wechat agent id","messagePattern":"Illegal wechat agent id","errorType":"exception","errorClass":"AuthException","httpStatus":null,"severity":"error","filePath":"src/main/java/me/zhyd/oauth/request/AuthWeChatEnterpriseQrcodeV2Request.java","lineNumber":48,"sourceCode":"    public String authorize(String state) {\n        return UrlBuilder.fromBaseUrl(source.authorize())\n            .queryParam(\"login_type\", config.getLoginType())\n            // 登录类型为企业自建应用/服务商代开发应用时填企业 CorpID，第三方登录时填登录授权 SuiteID\n            .queryParam(\"appid\", config.getClientId())\n            // 企业自建应用/服务商代开发应用 AgentID，当login_type=CorpApp时填写\n            .queryParam(\"agentid\", config.getAgentId())\n            .queryParam(\"redirect_uri\", GlobalAuthUtils.urlEncode(config.getRedirectUri()))\n            .queryParam(\"state\", getRealState(state))\n            .queryParam(\"lang\", config.getLang())\n            .build()\n            .concat(\"#wechat_redirect\");\n    }\n\n    @Override\n    protected void checkConfig(AuthConfig config) {\n        super.checkConfig(config);\n        if (\"CorpApp\".equals(config.getLoginType()) && StringUtils.isEmpty(config.getAgentId())) {\n            throw new AuthException(AuthResponseStatus.ILLEGAL_WECHAT_AGENT_ID, source);\n        }\n    }\n}\n","sourceCodeStart":30,"sourceCodeEnd":52,"githubUrl":"https://github.com/justauth/JustAuth/blob/694bbf1b010d93404e3bfb4824d90e9ddfaebebb/src/main/java/me/zhyd/oauth/request/AuthWeChatEnterpriseQrcodeV2Request.java#L30-L52","documentation":"AuthWeChatEnterpriseQrcodeV2Request.checkConfig validates the AuthConfig after the standard checks: when loginType equals \"CorpApp\" (enterprise self-built app mode), agentId must be non-empty, otherwise it throws AuthException with AuthResponseStatus.ILLEGAL_WECHAT_AGENT_ID (code 5016). The agentid is required because the v2 enterprise QR authorize URL embeds it as a query parameter.","triggerScenarios":"Constructing AuthWeChatEnterpriseQrcodeV2Request (or calling authorize on it) with loginType=\"CorpApp\" but AuthConfig.agentId null/empty. Occurs at request creation/authorization time, before any HTTP call.","commonSituations":"Copy-pasting a WeChat enterprise config from the (non-v2) qrcode request where agentId was optional in third-party ('ServiceProvider'/'第三方') mode; forgetting to set agentId for self-built apps; reading agentId from an env var that is unset in the deployed environment.","solutions":["Set agentId in AuthConfig to the numeric agent id of your enterprise self-built app (found in WeChat Work admin console > 应用管理 > the app's AgentId).","If you are actually a third-party service provider (服务商), set loginType to \"ServiceProvider\" instead of \"CorpApp\".","Fail fast at startup: validate the config (loginType/agentId pair) when building the AuthRequest rather than at authorize time."],"exampleFix":"// before\nAuthConfig cfg = AuthConfig.builder()\n    .clientId(\"ww1234 corpId\").clientSecret(\"secret\")\n    .redirectUri(\"https://app.example.com/wx/callback\")\n    .loginType(\"CorpApp\")\n    .build(); // agentId missing -> 5016\n\n// after\nAuthConfig cfg = AuthConfig.builder()\n    .clientId(\"ww1234corpId\").clientSecret(\"secret\")\n    .redirectUri(\"https://app.example.com/wx/callback\")\n    .loginType(\"CorpApp\")\n    .agentId(\"1000002\")\n    .build();","handlingStrategy":"validation","validationCode":"// fail fast at startup, not at authorize time\nvoid validateWeChatWorkV2(AuthConfig c) {\n    if (\"CorpApp\".equals(c.getLoginType()) && StringUtils.isEmpty(c.getAgentId())) {\n        throw new IllegalArgumentException(\"agentId is required when loginType=CorpApp\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    String url = request.authorize(state);\n} catch (AuthException e) {\n    if (e.getCode() == 5016) throw new ConfigurationException(\"WeChat Work: set agentId for CorpApp login\", e);\n    throw e;\n}","preventionTips":["Validate loginType/agentId pairs in a startup config check for all providers.","Bind agentId to an env var and assert it is present in the deployed profile.","Integration-test request construction for every provider in CI."],"tags":["justauth","wechat-work","config-error","agent-id","validation"],"backgroundTag":null,"analyzedSha":"694bbf1b010d93404e3bfb4824d90e9ddfaebebb","analyzedAt":"2026-08-14T15:16:59.945Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}