最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]Flask在Flask-Login和login_required后有时候出现微信授权登录token过期:OfficialAPIError 40001 invalid credential

Flask crifan 2564浏览 0评论

之前已经遇到过:

[已解决]微信授权后获取用户信息失败:40001 invalid credential access_token is invalid or not latest hints req_id

但是那是:

wechat的python的SDK,内部处理oauth的access_token和本身全局的access_token没考虑周全导致的

自己已经解决该问题了。

但是现在,在实现了授权登录,记住用户的情况下:

[记录]如何在Flask中实现记住微信公众号的用户帐户的登录状态

时不时的:

比如,在加入活动之前,要求微信用户已登录,但是这时候去授权登录,结果是用的旧的过期的access_token去登录的,所以会出现:

OfficialAPIError: 40001: invalid credential, access_token is invalid or not latest hint: [ir.EBa0605vr30!]

的错误:

postJson={‘action_info’: {‘scene’: {‘scene_str’: u’JOIN_EVENT_12′}}, ‘action_name’: ‘QR_LIMIT_STR_SCENE’}

<div–<——————————————————————————

[2016-09-02 21:23:25 +0000] [25734] [ERROR] Error handling request /create_event_qrcode?eventId=12
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/html/SIPEvents/sipevents/views.py”, line 724, in decorated_function
    return f(*args, **kwargs)
  File “/root/html/SIPEvents/sipevents/views.py”, line 938, in create_event_qrcode
    respQrcodeJson = wechat.create_qrcode(postJson)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/basic.py”, line 833, in create_qrcode
    data=data
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 104, in post
    **kwargs
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 74, in request
    self._check_official_error(response_json)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 113, in _check_official_error
    raise OfficialAPIError(errcode=json_data.get(‘errcode’), errmsg=json_data.get(‘errmsg’, ”))
OfficialAPIError: 40001: invalid credential, access_token is invalid or not latest hint: [ir.EBa0605vr30!]

目前出现的频率很高:

最短1,2小时左右?

最长好像是2天左右??

现在想要找到统一的,处理办法:

保证不会过期,或者很长时间,比如7天,1一个月之类的,也不会过期

目前能想到的,可能的办法是:

在调用generate_oauth2_authorize_url

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/wechat_sdk/basic.py

def generate_oauth2_authorize_url(self, redirect_uri, response_type=”code”, scope=”snsapi_userinfo”, state=””):
    “””
    生成获取用户信息的url
    详情请参考 http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html#.E7.AC.AC.E4.B8.80.E6.AD.A5.EF.BC.9A.E7.94.A8.E6.88.B7.E5.90.8C.E6.84.8F.E6.8E.88.E6.9D.83.EF.BC.8C.E8.8E.B7.E5.8F.96code
    :param redirect_uri: 授权后重定向的回调链接地址,该方法内自动使用urlencode对链接进行处理
    :param response_type: 返回类型,默认为code
    :param scope: 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
        snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。
        并且,即使在未关注的情况下,只要用户授权,也能获取其信息)。
        默认为snsapi_userinfo
    :param state: 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
    :return: 用户授权的url链接字符串
    “””
    redirect_uri = urllib.quote(redirect_uri)
    url = “https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=%s&scope=%s&state=%s#wechat_redirect” % (
        self.conf.appid,
        redirect_uri,
        response_type,
        scope,
        state
    )
    return url

去使用:

静默授权,先去获得openid

-》内部,在大多数情况下,已经保存了用户的完整的信息

-》此时可以通过openid去得到用户信息

-》这样就不用显示地跳转到授权登录的页面了

-》但是也还是有缺点的:

万一用户信息更新,比如头像更新了,此时也是没发及时知道,及时更新的

更主要的是:估计别人也很少用这个静默授权吧?

或者是:

在此处的调用

wechat.get_oauth2_userinfo(oauth2Access_token, oauth2Openid)

的地方,加上exception处理,发现如果是

OfficialAPIError: 40001

就重新再去执行一次授权

-》或者直接向上面一样,去静默授权获得openid,去从数据库中读取用户的信息,得到User对象

Flask OfficialAPIError: 40001: invalid credential

获得二维码{“errcode”:40001,”errmsg”:”invalid credential”} · Issue #142 · node-webot/wechat

解决多个位置存储微信公号api access_token值导致接口返回42001错误 | {流水理鱼|wwek}

wechat-python-sdk by doraemonext

Flask 40001 invalid credential

错误信息:”errcode”: 40001, “errmsg”: “invalid credential” – 微信公众平台开发者问答系统

微信公众平台获取凭证时总是返回{“errcode”:40001,”errmsg”:”invalid credential”},这个问题是怎么解决的? – 开源中国社区

javascript – 微信网页扫码登录不能获取用户信息 – SegmentFault

转载请注明:在路上 » [未解决]Flask在Flask-Login和login_required后有时候出现微信授权登录token过期:OfficialAPIError 40001 invalid credential

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.188 seconds, using 22.07MB memory