Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python code to scan the official account to log in

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use python code to scan official account login". In daily operation, I believe that many people have doubts about how to use python code to scan official account login. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to use python code to scan official account login". Next, please follow the editor to study!

General idea: call Wechat with parameter QR code interface to generate the QR code, the front end displays the QR code and communicates with the server with a long link at the same time, monitors the concern status, and logs in if there is a concern for the corresponding scenario code parameters.

The main code implementation:

Get the QR code part

1. Fetch access_token

App_id = 'xxxx'app_secret =' xxxxxx'url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}'.format(app_id, app_secret) resp = requests.get (url) rs = util.get_redis_con () access_token = resp.json () [' access_token'] print (access_token)

This access_token is valid for two hours, so it is recommended to write a scheduled task, execute it every hour, save it to redis, and redis it when you use it.

two。 Fetch ticket

My_scene_str = 'xxxxx' # scene code can make a key and put it in redis and set the same expiration time as the QR code url =' https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}'.format(access_token)data = {"expire_seconds": 604800, "action_name": "QR_STR_SCENE" "action_info": {"scene": {"scene_str": md5_user_key} resp = requests.post (url, json=data) ticket = resp.json () ['ticket']

3. Take the QR code image (when the picture is returned here, the scene code is also returned to the front end, so that if the front end uses polling to get attention status, the front end can not use long links)

Url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' + ticketresp = requests.get (url) img = base64.b64encode (resp.content) .decode (' ascii') print (img)

The image obtained in this is returned to the front end using base64, and the front end can be displayed using src=_ "data:image/png;base64," + img.

Get the value of interest status

Method 1: use scene code to poll

Send a request to poll md5_user_key

Method 2: use the scenario code to establish a long link and wait for the server to push

Flask recommends using socketio

Parsing Wechat server messages

New attention to the contents of users' scanning messages

1609128953

Regular users scan the contents of the code message

1609127524

ToUserName: the id of the official account

FromUserName: user's openid

Import xml.etree.ElementTree as ETroot = ET.fromstring (request.data.decode ('utf-8')) dic = {} for x in root: dic [x.tag] = x.textif dic.get (' MsgType') = = 'event': if dic.get (' Event') = = 'subscribe': parse_subscribe (dic) # New followers scan if dic.get (' Event') = = 'SCAN': parse_scan ( Dic) # has followed the user scanning code

Use openid to get the information of Wechat users you follow.

Url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={access_token}&openid={open_id}&lang=zh_CN"resp = requests.get (url.format (access_token=access_token, open_id=open_id)) resp.encoding = 'utf-8'return resp.json ()

Return parameters

{'subscribe': 1,' openid': 'xxxxxxxxxxxxxxxxxxx',' nickname': 'xxx',' sex': 1, 'language':' zh_CN', 'city':' xx', 'province':' xx', 'country':' xx', 'headimgurl':' http://thirdwx.qlogo.cn/mmopen/xxxxxxxxxxxxxxxxxx/xxx', 'subscribe_time': 1609128953,' unionid': 'xxxxxxxxxxxxxxxxx',' remark':' 'groupid': 0, 'tagid_list': [],' subscribe_scene': 'ADD_SCENE_QR_CODE',' qr_scene': 0, 'qr_scene_str':' xxxxxxxxxxxxxxxxxxxx'} so far The study on "how to use python code to scan the code to follow the official account login" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report