我的代码引起这个错误的原因是:前端和后端的AppID 和 AppSecret不匹配
还有一种环境会引起如下报错再另一个博客: 错误代码:40029, 错误信息:code 无效(多次调用 wx.login):https://blog.csdn.net/m0_65152767/article/details/144933124?sharetype=blogdetail&sharerId=144933124&sharerefer=PC&sharesource=m0_65152767&spm=1011.2480.3001.8118
- 2024-12-30 19:22:06.435 ERROR 4636 --- [nio-8087-exec-8] c.b.w.m.api.impl.BaseWxMaServiceImpl :
- 【请求地址】: https://api.weixin.qq.com/sns/jscode2session?access_token=87_kbs0BRvjVC3DdHKJu22E8eneERqtCYzF8zf8HmGWd1m_e4gdEXF66Btq209wfrkS_J4vbfG2dbXe2nme-UXCYFrEiAsELQrPMU_qj9mUgc8OytJbyigw4EEYonMPPPdABASSS
- 【请求参数】:js_code=0b1fO2100jpTrT1Ap2200omdMB3fO21N&secret=******&grant_type=authorization_code&appid=wxa1282c5b5fe20035
- 【错误信息】:错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
- 2024-12-30 19:22:06.439 ERROR 4636 --- [nio-8087-exec-8] c.p.a.controller.wechat.WxMaController : 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
- me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
- at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:296)
- at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.execute(BaseWxMaServiceImpl.java:222)
- at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.get(BaseWxMaServiceImpl.java:191)
- at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.jsCode2SessionInfo(BaseWxMaServiceImpl.java:125)
- at cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl.getSessionInfo(WxMaUserServiceImpl.java:33)
- at com.productQualification.api.controller.wechat.WxMaController.session(WxMaController.java:88)
- at com.productQualification.api.controller.wechat.WxMaController$$FastClassBySpringCGLIB$$16d10ffc.invoke(<generated>)
- at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
- at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
- at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
- at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
- at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
- at com.productQualification.api.controller.wechat.WxMaController$$EnhancerBySpringCGLIB$$f080276b.session(<generated>)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
- at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
- at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
- at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
- at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
- at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
- at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
- at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
- at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
- at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
- at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
- at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
- at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
- at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
- at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
- at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
- at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
- at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
- at java.lang.Thread.run(Thread.java:750)
- Caused by: me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
- at me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor.handleResponse(SimpleGetRequestExecutor.java:46)
- at me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor.execute(ApacheSimpleGetRequestExecutor.java:42)
- at me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor.execute(ApacheSimpleGetRequestExecutor.java:21)
- at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:268)
- ... 116 common frames omitted
- param:code
- args:0b1fO2100jpTrT1Ap2200omdMB3fO21N
- param:inviterId
- args:null
- param:httpSession
- args:org.apache.catalina.session.StandardSessionFacade@dd29db6
复制代码 以下是对你提供的错误信息的格式化剖析:
错误信息
- {
- "code": 1,
- "msg": "me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 677283c6-04105525-6d8ee3b6"}",
- "data": null
- }
复制代码 错误剖析
- 错误代码:
- 错误信息:
- invalid code:表示提供的 code 无效。
- 请求 ID:
- rid: 677283c6-04105525-6d8ee3b6:这是微信接口请求的唯一标识,用于排查题目。
大概的原因
- code 已逾期:
- 微信的 code 有用期为 5 分钟,如果高出时间未使用,会导致 code 失效。
- code 已被使用:
- 微信的 code 只能使用一次,如果重复使用,会导致 code 无效。
- code 获取错误:
- 前端获取 code 时大概出现了题目,导致通报到后端的 code 不精确。
- AppID 或 AppSecret 错误:
- 后端调用微信接口时,使用的 AppID 或 AppSecret 不精确,导致无法验证 code。
办理方法
- 检查 code 的有用性:
- 确保 code 在获取后 5 分钟内使用,且未被重复使用。
- 重新获取 code:
- 前端重新调用 wx.login 获取新的 code,并发送到后端。
- 检查 AppID 和 AppSecret:
- 确保后端调用微信接口时,使用的 AppID 和 AppSecret 精确无误。
- 查察微信接口文档:
- 参考微信官方文档,确认接口调用方式是否精确:
微信登录接口文档
示例代码
前端:重新获取 code
- wx.login({
- success(res) {
- if (res.code) {
- console.log('获取到的 code:', res.code);
- // 将 code 发送到后端
- sendCodeToServer(res.code);
- } else {
- console.log('获取 code 失败:', res.errMsg);
- }
- }
- });
复制代码 后端:调用微信接口
- // 使用微信 SDK 调用 code2Session 接口
- WxMaService wxMaService = WxMaConfiguration.getWxMaService();
- WxMaJscode2SessionResult sessionInfo = wxMaService.jsCode2SessionInfo(code);
- String openid = sessionInfo.getOpenid();
- String sessionKey = sessionInfo.getSessionKey();
复制代码 总结
该错误是由于 code 无效导致的,大概的原因是 code 逾期、重复使用或获取错误。通过重新获取 code 并检查 AppID 和 AppSecret,可以办理此题目。如果题目仍然存在,可以联合微信接口的 rid 进一步排查。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |