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://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=%E5%9F%BA%E7%A1%80%E6%94%AF%E6%8C%81&form=%E8%8E%B7%E5%8F%96access_token%E6%8E%A5%E5%8F%A3%20/token

2.python生成微信小程序码

# 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

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值