微信公众号 API 封装,支持素材管理和发布能力。
get_stable_access_token - 获取稳定版 Access Tokenadd_material - 上传永久素材(图片/视频/语音/缩略图)get_material - 获取永久素材add_draft - 新建草稿get_draft - 获取草稿列表delete_draft - 删除草稿submit_publish - 发布草稿get_publish_status - 发布状态查询get_published_articles - 获取已发布图文信息get_published_list - 获取已发布的消息列表delete_publish - 删除发布文章首次使用需要配置 AppID 和 AppSecret:
# 创建配置文件
cat > ~/.wechat_gzh_config.json << EOF
{
"appid": "your_appid_here",
"secret": "your_appsecret_here"
}
EOF
# 设置权限
chmod 600 ~/.wechat_gzh_config.json
from scripts.wechat_gzh import WeChatGZH
# 初始化
wechat = WeChatGZH()
# 获取 Access Token
token = wechat.get_stable_access_token()
# 上传永久素材
result = wechat.add_material("/path/to/image.jpg", "image")
print(f"Media ID: {result['media_id']}")
# 上传视频素材
result = wechat.add_material(
"/path/to/video.mp4",
"video",
video_info={"title": "我的视频", "introduction": "视频描述"}
)
# 获取已发布列表
articles = wechat.get_published_list(offset=0, count=10)
# 发布草稿
result = wechat.submit_publish(media_id="xxx")
# 获取 Access Token
python scripts/wechat_gzh.py get-token
# 上传永久素材
python scripts/wechat_gzh.py upload-material -f /path/to/image.jpg -t image
python scripts/wechat_gzh.py upload-material -f /path/to/video.mp4 -t video --title "视频标题"
# 获取已发布列表
python scripts/wechat_gzh.py list-published --offset 0 --count 10
# 发布草稿
python scripts/wechat_gzh.py publish --media-id xxx
# 查询发布状态
python scripts/wechat_gzh.py status --publish-id xxx
POST https://api.weixin.qq.com/cgi-bin/stable_tokenPOST https://api.weixin.qq.com/cgi-bin/material/add_materialPOST https://api.weixin.qq.com/cgi-bin/material/get_materialPOST https://api.weixin.qq.com/cgi-bin/draft/addPOST https://api.weixin.qq.com/cgi-bin/draft/batchgetPOST https://api.weixin.qq.com/cgi-bin/draft/deletePOST https://api.weixin.qq.com/cgi-bin/freepublish/submitPOST https://api.weixin.qq.com/cgi-bin/freepublish/getPOST https://api.weixin.qq.com/cgi-bin/freepublish/getarticlePOST https://api.weixin.qq.com/cgi-bin/freepublish/batchgetPOST https://api.weixin.qq.com/cgi-bin/freepublish/delete常见错误码:
40001: invalid credential - access_token 无效40007: invalid media_id - 无效的媒体 ID48001: api unauthorized - 接口未授权完整错误码参考:https://developers.weixin.qq.com/doc/oplatform/developers/errCode/errCode.html
# 1. 准备图文素材(需要先上传)
# 2. 创建草稿
draft = wechat.add_draft(articles=[{
"title": "标题",
"content": "正文内容",
"thumb_media_id": "封面图 media_id"
}])
# 3. 发布草稿
result = wechat.submit_publish(media_id=draft['media_id'])
# 4. 查询发布状态
status = wechat.get_publish_status(publish_id=result['publish_id'])
共 1 个版本