python之微信登陆KeyError: pass_ticket问题

背景

需求:当有异常情况时,通过python控制微信向自己发送消息。

当前python的微信第三方库比较成熟的有itchat,pywx等。浏览了一下他们的官网,pywx是基于itchat的封装,并且直接封装提供logger,可以给通过logger的方式调用来给指定的人发送微信消息推送。

于是选择pywx。

参考官网进行调用登陆时,刚扫登陆二维码就异常报错了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Loading the contact, this may take a little while.
Traceback (most recent call last):
File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3291, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-ba7301e4edc6>", line 1, in <module>
bot = Bot(console_qr=True)
File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/wxpy/api/bot.py", line 86, in __init__
loginCallback=login_callback, exitCallback=logout_callback
File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/register.py", line 35, in auto_login
loginCallback=loginCallback, exitCallback=exitCallback)
File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/login.py", line 66, in login
self.show_mobile_login()
File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/login.py", line 212, in show_mobile_login
self.loginInfo['url'], self.loginInfo['pass_ticket'])
KeyError: 'pass_ticket'

排查

  • 先尝试下直接调用原生的itchat登陆:

    1
    2
    import itchat
    itchat.auto_login()

    扫码果然也是异常:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Traceback (most recent call last):
    File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3291, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-6-c245a7379b4c>", line 1, in <module>
    itchat.auto_login()
    File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/register.py", line 35, in auto_login
    loginCallback=loginCallback, exitCallback=exitCallback)
    File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/login.py", line 66, in login
    self.show_mobile_login()
    File "/Users/zhengk/PycharmProjects/Mine/venv/lib/python3.7/site-packages/itchat/components/login.py", line 212, in show_mobile_login
    self.loginInfo['url'], self.loginInfo['pass_ticket'])
    KeyError: 'pass_ticket'
  • 在官网git的issue里搜索了一下,果然有提这个issue

    大意就是说微信现在把一些新账号做了限制网页登陆处理,从而导致出现上述的报错,用老的微信账号就不会有问题。

    https://github.com/youfou/wxpy/issues/340

  • 自己直接用网页版微信登陆测试:

    访问:https://wx.qq.com

    扫码登陆,果然是报错的

    image-20190602155759968

  • 使用自己的老微信号,尝试可以登录网页版后,在通过wxpy或itchat登陆,果然是登陆成功了。

    1
    2
    3
    4
    5
    6
    7
    8
    itchat.auto_login()
    Getting uuid of QR code.
    Downloading QR code.
    Please scan the QR code to log in.
    Please press confirm on your phone.
    Loading the contact, this may take a little while.
    TERM environment variable not set.
    Login successfully as XXX

总结与扩展

根据上述分析,结合Github上的issue。

的确是新注册的微信号被腾讯限制登陆网页版,而无论是wxpy还是itchat都是基于网页版微信的。只能使用注册的比较早的微信号了。

但是如果使用自己的主微信号,后续是否有被封号的危险,这个不得而知,不建议长期使用。

那么使用微信作为后台推送消息这条路差不多都封死了。

itchat项目上有介绍一个itchatmp项目,是控制微信公众号实现自动答复的,但是微信公众号是否有办法主动给用户推送消息。这个还有待研究。

更多内容可参考项目的官网:

itchat:https://itchat.readthedocs.io/zh/latest/

itchatmp:https://itchatmp.readthedocs.io/zh_CN/latest/

wxpy:http://wxpy.readthedocs.io