错误代码:40029, 错误信息:code 无效(前端和后端的AppID 和 AppSecret不 ...

打印 上一主题 下一主题

主题 992|帖子 992|积分 2976

我的代码引起这个错误的原因是:前端和后端的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
  1. 2024-12-30 19:22:06.435 ERROR 4636 --- [nio-8087-exec-8] c.b.w.m.api.impl.BaseWxMaServiceImpl     :
  2. 【请求地址】: https://api.weixin.qq.com/sns/jscode2session?access_token=87_kbs0BRvjVC3DdHKJu22E8eneERqtCYzF8zf8HmGWd1m_e4gdEXF66Btq209wfrkS_J4vbfG2dbXe2nme-UXCYFrEiAsELQrPMU_qj9mUgc8OytJbyigw4EEYonMPPPdABASSS
  3. 【请求参数】:js_code=0b1fO2100jpTrT1Ap2200omdMB3fO21N&secret=******&grant_type=authorization_code&appid=wxa1282c5b5fe20035
  4. 【错误信息】:错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
  5. 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"}
  6. me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
  7.         at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:296)
  8.         at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.execute(BaseWxMaServiceImpl.java:222)
  9.         at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.get(BaseWxMaServiceImpl.java:191)
  10.         at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.jsCode2SessionInfo(BaseWxMaServiceImpl.java:125)
  11.         at cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl.getSessionInfo(WxMaUserServiceImpl.java:33)
  12.         at com.productQualification.api.controller.wechat.WxMaController.session(WxMaController.java:88)
  13.         at com.productQualification.api.controller.wechat.WxMaController$$FastClassBySpringCGLIB$$16d10ffc.invoke(<generated>)
  14.         at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
  15.         at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
  16.         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
  17.         at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
  18.         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  19.         at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
  20.         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  21.         at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
  22.         at com.productQualification.api.controller.wechat.WxMaController$$EnhancerBySpringCGLIB$$f080276b.session(<generated>)
  23.         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  24.         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  25.         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  26.         at java.lang.reflect.Method.invoke(Method.java:498)
  27.         at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
  28.         at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
  29.         at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
  30.         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
  31.         at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
  32.         at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
  33.         at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
  34.         at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
  35.         at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
  36.         at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
  37.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  38.         at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
  39.         at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  40.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
  41.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  42.         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  43.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  44.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  45.         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
  46.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  47.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  48.         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
  49.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  50.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  51.         at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
  52.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  53.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  54.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
  55.         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
  56.         at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
  57.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  58.         at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
  59.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  60.         at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
  61.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  62.         at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
  63.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  64.         at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
  65.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  66.         at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
  67.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  68.         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
  69.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  70.         at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
  71.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  72.         at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
  73.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  74.         at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
  75.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  76.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  77.         at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
  78.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  79.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  80.         at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
  81.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  82.         at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
  83.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  84.         at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
  85.         at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
  86.         at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
  87.         at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
  88.         at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
  89.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  90.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  91.         at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  92.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  93.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  94.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  95.         at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
  96.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  97.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  98.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  99.         at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
  100.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  101.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  102.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  103.         at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
  104.         at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  105.         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  106.         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  107.         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
  108.         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  109.         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
  110.         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
  111.         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
  112.         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
  113.         at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
  114.         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  115.         at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
  116.         at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  117.         at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)
  118.         at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
  119.         at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  120.         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  121.         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  122.         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  123.         at java.lang.Thread.run(Thread.java:750)
  124. Caused by: me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 6772825e-1e404bd1-6c93b814"}
  125.         at me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor.handleResponse(SimpleGetRequestExecutor.java:46)
  126.         at me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor.execute(ApacheSimpleGetRequestExecutor.java:42)
  127.         at me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor.execute(ApacheSimpleGetRequestExecutor.java:21)
  128.         at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:268)
  129.         ... 116 common frames omitted
  130. param:code
  131. args:0b1fO2100jpTrT1Ap2200omdMB3fO21N
  132. param:inviterId
  133. args:null
  134. param:httpSession
  135. args:org.apache.catalina.session.StandardSessionFacade@dd29db6
复制代码
以下是对你提供的错误信息的格式化剖析:

错误信息

  1. {
  2.   "code": 1,
  3.   "msg": "me.chanjar.weixin.common.error.WxErrorException: 错误代码:40029, 错误信息:code 无效,微信原始报文:{"errcode":40029,"errmsg":"invalid code, rid: 677283c6-04105525-6d8ee3b6"}",
  4.   "data": null
  5. }
复制代码

错误剖析


  • 错误代码

    • 40029:这是微信接口返回的错误代码。

  • 错误信息

    • 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

  1. wx.login({
  2.   success(res) {
  3.     if (res.code) {
  4.       console.log('获取到的 code:', res.code);
  5.       // 将 code 发送到后端
  6.       sendCodeToServer(res.code);
  7.     } else {
  8.       console.log('获取 code 失败:', res.errMsg);
  9.     }
  10.   }
  11. });
复制代码
后端:调用微信接口

  1. // 使用微信 SDK 调用 code2Session 接口
  2. WxMaService wxMaService = WxMaConfiguration.getWxMaService();
  3. WxMaJscode2SessionResult sessionInfo = wxMaService.jsCode2SessionInfo(code);
  4. String openid = sessionInfo.getOpenid();
  5. String sessionKey = sessionInfo.getSessionKey();
复制代码

总结

该错误是由于 code 无效导致的,大概的原因是 code 逾期、重复使用或获取错误。通过重新获取 code 并检查 AppID 和 AppSecret,可以办理此题目。如果题目仍然存在,可以联合微信接口的 rid 进一步排查。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表