有三种接口 都要获取access_token
# todo 获取access_token
def get_token():
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(APPID,SECRET)
respon = requests.get(url)
content = respon.content
content = content.decode('utf-8')
data = json.loads(content)
return data.get("access_token")
- 接口 A: 适用于需要的码数量较少的业务场景
- 生成小程序码,可接受 path 参数较长,生成个数受限,数量限制见 注意事项,请谨慎使用。
- 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制
# todo getWXACode 获取二维码 # 获取小程序码,适用于需要的码数量较少的业务场景。通过该接口生成的小程序码,永久有效,有数量限制 def getWXACode(): access_token = get_token() if not access_token: pass else: url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token={}'.format(access_token) data = {"path":"pages/chargeDetail/chargeDetail?id=24", # todo 传参 "width":1280, "auto_color":False, "line_color":{"r":133,"g":218,"b":70}, # 自定义颜色 auto_color 为 false 时生效 # "line_color": {"r": 233, "g": 195, "b": 65}, # 自定义颜色 "is_hyaline":True # 是否需要透明底色 } # todo 不能使用data 要使用json # ret = requests.post(url, json=data) ret = requests.post(url,json=data) # print(ret.text) print(ret.content) with open('getWXACode1.png','wb') as f: f.write(ret.content)
- 接口 B:适用于需要的码数量极多的业务场景
- 生成小程序码,可接受页面参数较短,生成个数不受限。
# todo getWXACodeUnlimit 获取二维码
def getWXACodeUnlimit():
access_token = get_token()
if not access_token:
pass
else:
url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={}'.format(access_token)
data = {"scene":"15746151545fdfs56456",
"width":1280,
# "auto_color":True,
# "line_color":{"r":230,"g":40,"b":22}, # 自定义颜色
"line_color": {"r": 43, "g": 162, "b": 69}, # 自定义颜色
"is_hyaline":True}
# todo 不能使用data 要使用json
# ret = requests.post(url, json=data)
ret = requests.post(url,json=data)
# print(ret.text)
print(ret.content)
with open('getWXACodeUnlimit.png','wb') as f:
f.write(ret.content)
- 接口 C:适用于需要的码数量较少的业务场景
- 生成二维码,可接受 path 参数较长,生成个数受限,数量限制见 注意事项。
# todo createWXAQRCode 获取二维码
def createWXAQRCode():
access_token = get_token()
if not access_token:
pass
else:
url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={}'.format(access_token)
data = {"path":"pages/index/index",
"width":1280,}
# todo 不能使用data 要使用json
# ret = requests.post(url, json=data)
ret = requests.post(url,json=data)
# print(ret.text)
print(ret.content)
with open('createWXAQRCode.png','wb') as f:
f.write(ret.content)