{"record":{"id":"f35840194ef9dad4","repo":"justauth/JustAuth","slug":"url-wx-login-code","errorCode":null,"errorMessage":"不支持获取授权 url，请使用小程序内置函数 wx.login() 登录获取 code","messagePattern":"不支持获取授权 url，请使用小程序内置函数 wx\\.login\\(\\) 登录获取 code","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java","lineNumber":1447,"sourceCode":"        public String refresh() {\n            return \"https://www.figma.com/api/oauth/refresh\";\n        }\n\n        @Override\n        public Class<? extends AuthDefaultRequest> getTargetClass() {\n            return AuthFigmaRequest.class;\n        }\n    },\n    /**\n     * 微信小程序授权登录\n     * @since yudaocode\n     */\n    WECHAT_MINI_PROGRAM {\n\n        @Override\n        public String authorize() {\n            // 参见 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 文档\n            throw new UnsupportedOperationException(\"不支持获取授权 url，请使用小程序内置函数 wx.login() 登录获取 code\");\n        }\n\n        @Override\n        public String accessToken() {\n            // 参见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html 文档\n            // 获取 openid, unionId , session_key 等字段\n            return \"https://api.weixin.qq.com/sns/jscode2session\";\n        }\n\n        @Override\n        public String userInfo() {\n            // 参见 https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html 文档\n            throw new UnsupportedOperationException(\"不支持获取用户信息 url，请使用小程序内置函数 wx.getUserProfile() 获取用户信息\");\n        }\n\n        @Override\n        public Class<? extends AuthDefaultRequest> getTargetClass() {\n            return AuthWechatMiniProgramRequest.class;","sourceCodeStart":1429,"sourceCodeEnd":1465,"githubUrl":"https://github.com/justauth/JustAuth/blob/694bbf1b010d93404e3bfb4824d90e9ddfaebebb/src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java#L1429-L1465","documentation":"WECHAT_MINI_PROGRAM.authorize() throws UnsupportedOperationException with a Chinese message telling you to use the mini-program built-in wx.login() to obtain the code. WeChat mini programs have no web OAuth authorize URL: the code must come from the client-side wx.login() API, then be exchanged server-side via the jscode2session endpoint (this source's accessToken()).","triggerScenarios":"Calling authorize(state) / getAuthorizeUrl(...) on a WECHAT_MINI_PROGRAM-backed request; rendering a generic 'login with X' redirect page for every configured source including the mini program one.","commonSituations":"A unified login endpoint that iterates all configured providers and calls getAuthorizeUrl for each; front-end code accidentally initializing the web OAuth flow for a mini-program integration; note UnsupportedOperationException is unchecked and not wrapped in AuthException, so generic AuthException handlers will miss it.","solutions":["For WECHAT_MINI_PROGRAM, call wx.login() inside the mini program client and POST the resulting code to your server, which calls getAccessToken(AuthCallback with code).","Exclude mini-program sources from any authorize-URL generation flow (guard by source name/type).","Catch UnsupportedOperationException in addition to AuthException around generic provider loops."],"exampleFix":"// before\nString url = request.authorize(state); // throws UnsupportedOperationException for WECHAT_MINI_PROGRAM\n\n// after (mini-program client)\n// wx.login({ success: r => POST /auth/wechat-mini?code=r.code })\n// server:\nAuthToken token = request.getAccessToken(AuthCallback.builder().code(code).build());","handlingStrategy":"validation","validationCode":"if (\"wechat_mini_program\".equalsIgnoreCase(sourceName)) {\n    // no authorize URL: client calls wx.login() and posts the code\n    throw new ApiResponse(\"use wx.login() in the mini program client\");\n}","typeGuard":"boolean hasAuthorizeUrl(AuthSource s) {\n    try { s.authorize(); return true; }\n    catch (UnsupportedOperationException | AuthException e) { return false; }\n}","tryCatchPattern":"catch (UnsupportedOperationException e) { /* mini-program source: switch to wx.login() + getAccessToken flow */ }","preventionTips":["Keep an explicit allowlist of sources that support web authorize URLs.","Catch UnsupportedOperationException as well as AuthException around generic provider loops.","Mini-program flows start on the client, never with getAuthorizeUrl."],"tags":["justauth","wechat","mini-program","unsupported-operation","provider-quirk"],"backgroundTag":null,"analyzedSha":"694bbf1b010d93404e3bfb4824d90e9ddfaebebb","analyzedAt":"2026-08-14T15:16:59.945Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}