@
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地址
个人公众号: