微信公众号上传图文素材thumb_media_id的获得

本人最近在做一个微信公众号的java开发,其中有一个上传图文素材的功能,微信API文档中有一个thumb_media_id的参数,我一开始用的是上传图片获得的media_id,发现报40007的错误,在网上找了几篇文章看了也没什么收获,最后自己试试了https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE这个,把其中的type设置为thumb,其返回的media_id,就可以当作图文素材中的thumb_media_id。image类型上传的media_id在这是无效的。同时thumb图片大小是有限制的文档说是64k jpg格式的,供大家参考。

{

  "articles": [{

       "title": TITLE,

       "thumb_media_id": THUMB_MEDIA_ID,

       "author": AUTHOR,

       "digest": DIGEST,

       "show_cover_pic": SHOW_COVER_PIC(0 / 1),

       "content": CONTENT,

       "content_source_url": CONTENT_SOURCE_URL

    },

    //若新增的是多图文素材,则此处应还有几段articles结构

 ]

}


### 微信公众号 YOUR_BIZ_ID 开发文档 API 使用教程 #### 获取 Access Token 为了调用微信接口,开发者需要先获得 `access_token` 参数。这是微信公众平台提供的一种凭证,用于证明开发者身份的有效性和合法性[^3]。 ```python import requests def get_access_token(appid, secret): url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}" response = requests.get(url).json() access_token = response['access_token'] return access_token ``` 此函数接收两个参数:`appid` 和 `secret`,它们分别对应于微信开放平台上创建的应用 ID 和密钥。通过 GET 请求访问指定 URL 并解析 JSON 响应体中的数据即可得到所需的 `access_token`。 #### 发布图文消息 利用上述获取到的 `access_token`,可以进一步发布图文消息至订阅号或服务号中: ```python def send_article(access_token, title, description, url, image_url): post_data = { "articles": [ { "title": title, "thumb_media_id": "", # 需预先上传图片素材以取得 media_id, "author": "", "digest": description, "show_cover_pic": 1, "content": "<p>请点击链接查看:<a href='" + url + "'>" + url + "</a></p>", "url": url, "content_source_url": "" } ] } headers = {'Content-Type': 'application/json'} api_endpoint = f'https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={access_token}' result = requests.post(api_endpoint, json=post_data, headers=headers) return result.json() ``` 这段代码展示了如何构建 POST 请求的数据结构并向微信公众平台提交一篇新的文章。需要注意的是,“thumb_media_id”字段应该填入事先已上传过的封面图对应的媒体文件 ID;而 “content” 字段则允许嵌套 HTML 标签以便更好地格式化内容显示效果。 #### 用户管理功能 除了信息发布外,还可以借助 API 实现更多实用的功能,例如拉取关注者列表、设置菜单项等。下面的例子演示了怎样批量获取粉丝 OpenID 列表的方法之一: ```python def fetch_followers(openid_list=None, next_openid=""): params = {"access_token": get_access_token()} if openid_list is not None and isinstance(openid_list, list): data = {"user_list": [{"openid": oid} for oid in openid_list]} endpoint = "/cgi-bin/user/info/batchget" elif next_openid != "": data = {"next_openid": next_openid} endpoint = "/cgi-bin/user/get" resp = requests.post(f"https://api.weixin.qq.com{endpoint}", params=params, json=data) followers_info = [] try: users = resp.json().get('data', {}).get('list') for user in users: follower = {} follower["nickname"] = user.get("nickname", "") follower["city"] = user.get("city", "") follower["province"] = user.get("province", "") follower["country"] = user.get("country", "") follower["headimgurl"] = user.get("headimgurl", "") followers_info.append(follower) has_more = bool(resp.json().get('has_more')) new_next_openid = resp.json().get('next_openid') return followers_info, has_more, new_next_openid except Exception as e: print(e) return [], False, "" # Example usage of fetching all followers' information with pagination support. all_followers = [] more_to_fetch = True last_fetched_oid = "" while more_to_fetch: batch_result, more_to_fetch, last_fetched_oid = fetch_followers(next_openid=last_fetched_oid) all_followers.extend(batch_result) print(all_followers) ``` 该脚本实现了分页抓取所有关注者的个人信息,并将其存储在一个 Python 列表对象里供后续处理使用。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值