current position:Home>Python code to scan code to pay attention to official account login

Python code to scan code to pay attention to official account login

2022-01-30 16:10:11 Ice and wind all over the sky

  General train of thought : Call wechat QR code interface with parameters to generate QR code , The front end displays the QR code and carries out long link communication with the server at the same time , Monitor attention status , If there are concerns about the corresponding scene code parameters , Then log in .

The following is the service diagram

Main code implementation :

Get QR code part

1. take 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)
 Copy code 

This access_token The term of validity is two hours , So it is suggested to write a scheduled task , Execute every hour , Deposit in redis, Use it every time redis Take it .

2. take ticket

my_scene_str = 'xxxxx'  #  The scene code can be used as key Put it in redis And set an expiration time as 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']
 Copy code 

3. Take QR code picture ( When the picture is returned here, the scene code is also returned to the front end , In this way, if the front end uses polling to obtain the attention status, it can not use long links )

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

The pictures taken from this are base64 Back to the front end , Front end use src="data:image/png;base64,"+img You can show

Get the attention status value

Method 1 : Use scenario code polling

Send request polling md5_user_key

Method 2 : Use scene code to establish long link connection , Wait for the server to push

flask It is recommended to use socketio

Analyze wechat server message

  New attention is paid to the content of user code scanning message

<xml>
<ToUserName><![CDATA[xxxxxxxxxxxxx]]></ToUserName>
<FromUserName><![CDATA[xxxxxxxxxxx]]></FromUserName>
<CreateTime>1609128953</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[xxxxxx]]></EventKey>
<Ticket><![CDATA[xxxxxxxx]]></Ticket>
</xml>
 Copy code 

  Content of code scanning message of old users

<xml>
<ToUserName><![CDATA[xxxxxxxxxxxx]]></ToUserName>
<FromUserName><![CDATA[xxxxxxxxxxxxx]]></FromUserName>
<CreateTime>1609127524</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[SCAN]]></Event>
<EventKey><![CDATA[xxxxxxxxxxxxxx]]></EventKey>
<Ticket><![CDATA[xxxxxxxxxxxxxxxxxxx]]></Ticket>
</xml>
 Copy code 

ToUserName: Members of the public, id

FromUserName: User openid 

import xml.etree.ElementTree as ET
root = ET.fromstring(request.data.decode('utf-8'))
dic = {}
for x in root:
    dic[x.tag] = x.text

if dic.get('MsgType') == 'event':
    if dic.get('Event') == 'subscribe':
        parse_subscribe(dic)  #  Scan the code for new users 
    if dic.get('Event') == 'SCAN':
        parse_scan(dic)  #  We have paid attention to the user scanning code 
 Copy code 

Use openid Get the concerned wechat user information

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()
 Copy code 

 Returns the parameter 
{'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'}
 Copy code 

\

copyright notice
author[Ice and wind all over the sky],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301610086435.html

Random recommended