Giter Club home page Giter Club logo

Comments (46)

vipcgh avatar vipcgh commented on June 27, 2024 2

代码就在上面啊,就加了一句:
//修改建议,重发时清除token,重新获取
if(retryed){
token = null;
}

from wechat-api.

philipwabc avatar philipwabc commented on June 27, 2024 1

但是提前可能有半小时甚至一小时。

这种情况下,可以再检查一下token的管理,比如是否有不同的中间件在各自获取token?

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

发 Pull request

from wechat-api.

vimer avatar vimer commented on June 27, 2024

的确有时候 经常会出现 40001 错误 不知道有没有fix掉

from wechat-api.

XGHeaven avatar XGHeaven commented on June 27, 2024

我觉得你这个token=null没有什么太大的意义,以为后文根本用不到。。

from wechat-api.

looping84 avatar looping84 commented on June 27, 2024

最近经常碰到40001错误

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

@XGHeaven 确实没什么用。

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

情况是这样的:微信获取token时带过来的过期时间参数和实际过期不一致,经常提前很多时间就产生40001错误,原来的代码判断token过期是根据参数计算的,虽然有10秒冗余,但是提前可能有半小时甚至一小时。这时候重发消息,没有清除token的话,判断token仍然是有效的,会继续收到40001错误。加上token=null之后,会重新从微信取token,重发是就没问题了。

from wechat-api.

XGHeaven avatar XGHeaven commented on June 27, 2024

@vipcgh 你这个token只是一个形参,你改了并没有修改本质的token,你再次调用getToken,依旧还是有的。

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

这个代码已经经过验证有效。主要是下面的条件判断:
if (token && (accessToken = AccessToken(token.accessToken, token.expireTime)).isValid())
把token置为null后,会走到后面的分支,重新从微信取token

from wechat-api.

XGHeaven avatar XGHeaven commented on June 27, 2024

@vipcgh 好像有道理的样子。

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

我觉得关键还是你没有做好token的统一管理。

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

微信企业号官方有通知的,出于运营需要如更换密钥、系统维护,可能会在token未过期时强制令其失效。我们在实际场景中也已经遇到多次。解决这个问题的办法就是要在收到40001错误时,重新获取token。

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

= = 这个不是企业号的呀

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

机制是一样的

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

企业版的token跟这个完全不一样的。

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

我们的服务号也有这个问题,经常遇到,修改后已经解决了

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

微信服务器升级维护等等,都会导致token提前过期。逻辑上也是收到40001错误就应该更新token,否则重发时还用原来的token没有意义。

from wechat-api.

vimer avatar vimer commented on June 27, 2024

@vipcgh 我根据你这个方式加了
if(retryed){
token = null;
}

目前可以保证token不过期 ,但是大概10几分钟机会刷新一次token,导致一天的token数目不够用

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

你是不是多核或多台服务器?如果是,需要把token存入数据库。
如果不是,单进程每天2000次(测试服务器200次)的调用次数也够了。
大部分情况下,没有那么频繁,我们经常遇到在1小时,最短半小时左右出现。如果是10几分钟,肯定是微信服务器在升级维护造成。
另外,认证服务号每月有10次清零的机会。

from wechat-api.

vimer avatar vimer commented on June 27, 2024

@vipcgh 就算多进程(非多核)共用一个token,按照微信的描述是 2小时的过期时间,为什么会存在半小时刷一次的情况(除了微信服务器在升级维护)

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

这个就是微信服务器的问题了,我也不知道。从官方描述是存在升级维护时会造成提前过期的问题。

from wechat-api.

vimer avatar vimer commented on June 27, 2024

@vipcgh 也不可能一直在维护啊

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

我们碰到的次数不是很多,有时候集中几天会比较频繁,不是每天都这样。

from wechat-api.

superlbr avatar superlbr commented on June 27, 2024

talk is cheap , show me the code...

from wechat-api.

Messilimeng avatar Messilimeng commented on June 27, 2024

可以借鉴 Senparc.Weixin.MP 中的容错机制
qq 20160505133154
发现错了会自动重新获取一次,整个过程不会中断

from wechat-api.

JoeShi avatar JoeShi commented on June 27, 2024

+1

from wechat-api.

benjamin555 avatar benjamin555 commented on June 27, 2024

为啥还不合并代码,修复bug呀

from wechat-api.

olunx avatar olunx commented on June 27, 2024

亲,赶紧解决这个问题呀。

from wechat-api.

JoeShi avatar JoeShi commented on June 27, 2024

+1

from wechat-api.

Uncle-chan avatar Uncle-chan commented on June 27, 2024

这确实是个问题,只要access_token提前过期(比10s冗余还要早),就会发生使用api全部都是返回err,不会自动去获取最新的access_token,这是个比较严重的问题啊。

from wechat-api.

superlbr avatar superlbr commented on June 27, 2024

几乎肯定是全局维护的问题,但是我觉得fs.writeFIle这个方式也不算nice,虽然还在纠结要不要wirteFileSync

from wechat-api.

superlbr avatar superlbr commented on June 27, 2024

看到过把access_token放redis,用中间件去获取的方式。https://github.com/tangramor/wx_jsapi_sign

from wechat-api.

cage1618 avatar cage1618 commented on June 27, 2024

微信服务器维护造成token提前过期在哪里有说明吗?文档里没找到

from wechat-api.

superlbr avatar superlbr commented on June 27, 2024

还有如果全局维护,记得调用api.getLatestToken方法。 即使微信服务器维护,也不会token提前到期吧@cage1618

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

@superlbr 我们这个机制是给你一个接口。你怎么存怎么取,都可以。文件在这里只是个例子,你可以用mongodb、redis、memcached。。。

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

getLatestToken会自动调用。如果不放心,可以在进程启动的时候主动调用一次。

from wechat-api.

cage1618 avatar cage1618 commented on June 27, 2024

@superlbr 我是从上面的对话看有提到微信服务器维护会导致token提前过期的,所以问问在哪里找到官方的描述,如果真的会导致过期,微信要能推送个服务器维护的事件就好了

from wechat-api.

cage1618 avatar cage1618 commented on June 27, 2024

我项目里用到的rabbitmq,索性直接把token放内存了,刷新token的时候用rmq通知其他机器或进程更新

from wechat-api.

aojiaotage avatar aojiaotage commented on June 27, 2024

我们的项目里用的redis,取到accesstoken的过期时间之后,流出一定的冗余,比如十数分钟,然后直接给redis里的accesstoken设置一个过期,这样取不到token的时候就会重新取的

from wechat-api.

vipcgh avatar vipcgh commented on June 27, 2024

过期时间是不固定的,有些时候半小时就过期了,所以这个方案没有用的

from wechat-api.

qia0756 avatar qia0756 commented on June 27, 2024

同意@vipcgh 说的,这不是全局管理的问题,当有不同服务商都在维护accesstoken的时候,就容易出现这种问题。token唯一导致可能会被其它服务刷新,这样已经缓存的token就会提前过期,从而出现40001错误。的确需要对40001错误重新获取token,不然相关操作会全部中断。虽然有可能会超出腾讯调用次数,但是起码在必要时能重启好服务。

from wechat-api.

hardenCN avatar hardenCN commented on June 27, 2024

@vipcgh 谢谢

from wechat-api.

gxsjtu avatar gxsjtu commented on June 27, 2024

这么严重的问题 还不赶紧merge,没人维护了么

from wechat-api.

javaxiu avatar javaxiu commented on June 27, 2024

我现在也遇到这种问题.希望能有40001之类的err token过期后自动刷新

from wechat-api.

JacksonTian avatar JacksonTian commented on June 27, 2024

这个问题不会合并。

  1. wechat-api 自身已经有了 40001 的自动刷新重试。如果 token 是统一管理,正常过期的话,wechat-api 已经能够正常处理,因为 Token.isvalid() 会生效。
  2. 如果你有多个地方管理 token,在别处让 token 过期,导致 wechat-api 无法正常处理。建议想办法统一管理 token。

如果是在别出管理 token,想注入到 wechat-api,请 hack getToken/setToken,注入从别处获取的 token。
如果是在 wechat-api 中管理 token,需要分享到别的地方,请使用 getLatestToken 接口获取最新的 token。

from wechat-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.