python生成微信小程序码

1.获取用户access_token

# 传入字段为AppID,AppSecret
def get_access_token(appid,secret):
      access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid +                                                             
                                        "&secret=" + secret
      async with aiohttp.ClientSession(headers=None) as session:
              result = await session.get(url, timeout=60)
              result = await session.json()
              token = result['access_token']
       return token

注:微信提供小程序获取access_token文档地址

https://developers.weixin.qq.com/miniprogram/dev/api-backend/auth.getAccessToken.html

2.python生成微信小程序码

python生成微信小程序码_第1张图片

# data:{"path":"https://www.baidu.com","scene":""}
def wx_code(access_token,data):
         # 微信小程序码生成
         url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + access_token
         async with aiohttp.ClientSession(headers=None) as session:
         result = await session.post(url, timeout=60, data=json.dumps(data))
         result = await result.read()
         b64str = base64.b64encode(result)
         imgdata = base64.b64decode(b64str)
         time = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
         # 上传路径
         path = "/" + time + "wechat" + ".png"
         file = open(os.path.join(path, ), 'wb+')
         file.write(imgdata)
         file.close()
         return path

注:微信提供小程序生成小程序码接口文档

https://developers.weixin.qq.com/miniprogram/dev/api-backend/wxacode.getUnlimited.html

 

你可能感兴趣的:(python生成微信小程序码)