Python获取小程序二维码的三种方式

 代码很容易……


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)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值