Skip to content

access_token 过期之后没有重新获取 #37

Description

@mangojuju

微信企业号的某个agent在access_token 过期之后,就没调用setToken方法。 access_token 是存在redis 里的, 设置的两小时过期。
下面是错误log。

  • log
[2018-01-10 10:55:23]10.0.1.94
content: [2018-01-10 10:55:23.577] [INFO] app - begin e93f55ba-9a6c-4738-a668-8bc7a475534d POST /message/send ::ffff:10.0.1.93 
body: {"to":{"touser":"3"},"agentid":"agentid","message":{"msgtype":"text","text":{"content":"xxx"},"safe":"0"}}

[2018-01-10 10:55:23]10.0.1.94content: [2018-01-10 10:55:23.580] [INFO] app - get access token by redis: token = null

|[2018-01-10 10:55:23]10.0.1.94content: [2018-01-10 10:55:23.796] [ERROR] app - wechatAPI response error: method=send, error=invalid credential, hint: [1515552923_6_1c32f77d5bb48e4e928ca8e9c257f188], more info at https://open.work.weixin.qq.com/devtool/query?e=40001 code=40001
[2018-01-10 10:55:23]10.0.1.94content: [2018-01-10 10:55:23.797] [INFO] app - end e93f55ba-9a6c-4738-a668-8bc7a475534d POST /message/send - 500 - 220.157 ms
  • 代码
class WechatApi{
  constructor({corpId, secret, agentid}){
    this.api = new API(corpId, secret, agentid,
      (cb) => this._getToken(agentid).then( token=> cb(null, token)).catch( err => cb(err, null) ),
      (token, cb) => this._setToken(agentid, token).then( token=> cb && cb(null, token)))
  }

  _getToken(agentid) {
    // 取redis里token
    return redis.client('get', accessTokenPrefix+agentid)
      .then( token=>{
        logger.info(` get access token by redis: token =`, token)
        return JSON.parse(token)
      })
  }

  _setToken(agentid, token) {
    logger.info(` set access token: token = `, token)
    // 经过expire时间后过期
    redis.client('set', accessTokenPrefix+agentid, JSON.stringify(token), 'EX', expire)
    return Promise.resolve(token)
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions