代码很容易……
import json
import requests
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Cache-Control': 'no-cache',
'Content-Length': '189',
'Content-Type': 'application/json',
"Host": "api.weixin.qq.com",
"Connection": "keep-alive",
"cache-control": "no-cache"
}
#
appid = {"appid": "XXXXXXXXXXXXXXXXXXX",
"secret": "XXXXXXXXXXXXXXXXXXX"}
wxData = {
"path": "pages/authorization/index",
"width": 430,
"auto_color": False,
"line_color": {"r": "0", "g": "0", "b": "0"},
"scene": "bizType=1,serial=807d3a1e8618"
}
# 获取token
def getToken(data):
try:
url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + data[
"appid"] + "&secret=" + data["secret"] + ""
data = requests.get(url, data=data,
headers=headers)
except:
return None
else:
jsons = json.loads(data.text)
return jsons
def getWXACodeUnlimit(wxData):
# 获取token
tokenData = getToken(appid)
token = tokenData["access_token"]
if not token:
pass
else:
url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={}'.format(token)
# todo 不能使用data 要使用json
# ret = requests.post(url, json=data)
ret = requests.post(url, json=wxData)
# print(ret.text)
print(ret.content)
with open('getwxacodeunlimit.png', 'wb') as f:
f.write(ret.content)
def createWXAQRCode():
tokenData = getToken(appid)
token = tokenData["access_token"]
if not token:
pass
else:
url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={}'.format(token)
data = {"path": "pages/index/index",
"width": 430, }
# todo 不能使用data 要使用json
ret = requests.post(url, json=data)
print(ret.content)
with open('createwxaqrcode.png', 'wb') as f:
f.write(ret.content)
def getWxCode(wxData):
# 获取token
tokenData = getToken(appid)
token = tokenData["access_token"]
print(token)
url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token={}'.format(token)
try:
data = requests.post(url, json=wxData)
except:
return None
else:
with open('{}.png'.format(wxData["scene"].split(",")[1].split("=")[1]), 'wb') as f:
f.write(data.content)
if __name__ == '__main__':
getWXACodeUnlimit(wxData)
getWxCode(wxData)
createWXAQRCode()
这里只需要注意一点,将data改为json即可:
data = requests.post(url, data=wxData)