zoukankan      html  css  js  c++  java
  • 个人微信公众号搭建Python实现 -个人公众号搭建-永久素材管理(14.3.5)

    @

    1.说明

    个人微信公众号开发的功能有限,因为很多权限没有,但支持上传永久素材,具体查看微信公众号文档
    这里的请求都要将本地IP地址放到微信公众号的白名单

    2.上传素材

    从配置文件读取用户个人资料,用于获得访问api的权限
    使用的是官方演示的curl命令

    #执行这个步骤的时候记住把本机ip放入白名单
    import os
    import requests
    import json
    with open(r'../../resource/SET.json', 'r', encoding="utf-8") as f:
        SET = json.load(f)
        APPID = SET["APPID"]
        APPSECRET = SET["APPSECRET"]
    url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (APPID,APPSECRET)
    response_token = requests.post(url=url_token)
    access_token = json.loads(response_token.text)["access_token"]
    pic_dir = input("请输入要上传文件的绝对路径:")
    #参考官方文档,调用的cmd的curl命令
    cmd = 'curl -F media=@%s "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"' % (pic_dir,access_token)
    print(os.system(cmd))
    

    3.获取素材列表

    获取素材的目的,是为了获取到素材的唯一ID,因为想要发图片等信息给用户时,必须使用微信公众号分配的独立ID

    #执行这个步骤的时候记住把本机ip放入白名单
    import requests
    import json
    with open(r'../../resource/SET.json', 'r', encoding="utf-8") as f:
        SET = json.load(f)
        APPID = SET["APPID"]
        APPSECRET = SET["APPSECRET"]
    type = input("请输入素材的类型,image,video,voice,news:")
    offset = input("请输入开始位置(从全部素材的该偏移位置开始返回,0表示从第一个素材返回):")
    count = input("请输入素材的数量,取值在1到20之间:")
    data= {
        "type": type,
        "offset": int(offset),
        "count": int(count)
    
    }
    url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % (APPID,APPSECRET)
    response_token = requests.post(url=url_token)
    access_token = json.loads(response_token.text)["access_token"]
    url_list = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s" % access_token
    response_list = requests.post(url=url_list,data=json.dumps(data))
    for i in json.loads(response_list.text)["item"]:
        print(i)
    
    
    

    关于作者

    个人博客网站
    个人GitHub地址
    个人公众号:
    在这里插入图片描述

  • 相关阅读:
    MYSQL性能优化的最佳20+条经验
    MySQL性能分析工具之PROFILE
    理解事务的4种隔离级别
    二进制中1的个数
    滑动窗口最大值
    字符流中第一个不重复字符
    字符串转化为整数
    java字符,字符串,数字之间的转换
    java中数组输出的方式
    java基础知识(1)
  • 原文地址:https://www.cnblogs.com/simon-idea/p/11397718.html
Copyright © 2011-2022 走看看