python、请求微信V3版本 平台证书接口

直接上代码

url = "https://api.mch.weixin.qq.com/v3/certificates"

nonce_str = get_nonce_str(32)  #生成32位随机数
timestamp = str(int(time.time())) #时间戳

#生成签名,详细查看(https://www.cnblogs.com/mrzhao520/p/14548276.html)
sign = calculate_sign(data = "", method = "GET", url_path = "/v3/certificates", timestamp=timestamp, nonce_str = nonce_str)


Authorization = 'WECHATPAY2-SHA256-RSA2048 ' \
            'mchid="{mchid}",' \
            'nonce_str="{nonce_str}",' \
            'signature="{sign}",' \
            'timestamp="{timestamp}",' \
            'serial_no="{serial_no}"'.\
            format(mchid="商户号",
                   nonce_str="随机字符串(必须和签名中一致)",
                   sign=sign,
                   timestamp="时间戳(必须和签名中一致)",
                   serial_no="证书序列号"
                  )
headers = {
    'Content-Type': "application/json",
    'Accept': 'application/json',
    'Authorization': Authorization,
}

async def send_get(self, url, **kwargs):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, **kwargs) as response:
            return await response.json()

data = await send_get(url, headers = headers)

得到的json数据中可以直接拿到 证书序列号, 使用AEAD_AES_256_GCM解密得到证书公匙(用来验证微信header携带的签名),详细看:https://www.cnblogs.com/mrzhao520/p/14548253.html

nonce = data["data"][0]['encrypt_certificate']['nonce']
ciphertext = data["data"][0]['encrypt_certificate']['ciphertext']
associated_data = data["data"][0]['encrypt_certificate']['associated_data']

decrypt(
    nonce = nonce,
    ciphertext = ciphertext,
    associated_data = associated_data
)

posted @ 2021-03-17 11:32  做个笔记  阅读(625)  评论(0编辑  收藏  举报