获取公众号文章消息链接 (JAVA)
需求: APP用户分享视频后生成自己的带货链接。
坑爹啊 这链接从哪来的。。。去看微信公众号文档又是一头雾水。
整理:(不一定是用我这种方法,但我找不到其他方法了。)
思路:首先知道这个链接可以在 公众号给关注的用户群发消息成功后,在公众号的回调地址中可以解析xml后得到。
微信公众号文档地址: https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Batch_Sends_and_Originality_Checks.html
那么怎么给公众号用户群发消息呢?
具体步骤如下:
说明:我使用的是Hutool的HTTP连接工具,以下步骤都可以在微信公众号文档中找到,这里使用的是发送图文消息为例子。
Hutool文档地址:https://www.hutool.cn/docs/#/
第一步、使用公众号的appId和secret去获取微信公众号的access_token
access_token是访问公众号接口的唯一凭证,注意每天获取access_token的次数有限,目前每个token的存活时间为两个小时,灵活使用。
grant_type:固定值"client_credential"
appid:公众号的appid (可以在公众号管理平台的基本配置中找到)
secret:公众号的secret (同上)
请求地址:https://api.weixin.qq.com/cgi-bin/token
public String makeWxMpAccessToken() {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", WX_MP_APP_ID);
paramMap.put("secret", WX_MP_SECRET);
String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token", paramMap);
JSONObject parse = JSONObject.parseObject(result);
return parse.getString("access_token");
}
第二步、上传图文消息的缩略图 获取 thumb_media_id(一会上传图文消息要用到)
access_token是访问公众号接口的唯一凭证
type:这里上传类型选择 “thumb_media_id” 因为一会得用这个类型去发布图文消息。
media:选择上传的文件,这里是文件流的形式也就是File。 大小限制在64KB一下。如果是图片连接要先下载下来在以文件的方式上传。
请求地址:https://api.weixin.qq.com/cgi-bin/media/upload
/** 上传缩略图素材(为了获取缩略图的media_id)*/
public String getSmellImgMediaId(String imgUrl) {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("access_token", WX_MP_ACCESS_TOKEN);
paramMap.put("type", "thumb_media_id");
paramMap.put("media", FileUtil.file("\\opt\\webapps\\WxMpImg/1.jpg"));//文件路径
String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/media/upload", paramMap);
JSONObject parse = JSONObject.parseObject(result);
String thumb_media_id = parse.get("thumb_media_id").toString();
redisUtil.set("thumb_media_id",thumb_media_id,60*60*24*2);
System.out.println("缩略图id" + thumb_media_id);
return thumb_media_id;
}
第三步、上传图文消息 获取 media_id 这里稍微麻烦一点 分两个步骤:
3.1 设置参数
请求地址:https://api.weixin.qq.com/cgi-bin/media/uploadnews
articles: Articles对象 要注意格式 要注意格 式要注意格式
thumb_media_id:上一步获取的到的缩略图Id
author: 公众号图文消息显示的作者名称。
title:标题
content:内容 (放在下一个步骤讲)
digest:消息简介
content_source_url:点击阅读原文后跳转的链接地址
need_open_comment: 打开评论 (可以不设置)
only_fans_can_commen: 是否只有粉丝能评论(可以不设置)
show_cover_pic: 是否显示缩略图
3.2 设置正文内容
在content中可以插入小程序卡片等跳转小程序的方式 (文字跳转小程序格式如下代码:)
content: 正文内容
data-miniprogram-appid: 小程序的appid
data-miniprogram-path: 小程序跳转的页面
<p><a data-miniprogram-appid="wx123123123" data-miniprogram-path="pages/index" href="">点击文字跳转小程序</a></p>
3.3 该部分完整代码如下
/** 上传图文素材获取图文素材的media_id**/
public String upLoadImageAndTextToWx(String title, String richText, String remark, String smellImgMediaId) {
JSONObject Articles = new JSONObject();
JSONArray articles = new JSONArray();
JSONObject item = new JSONObject();
item.put("thumb_media_id", smellImgMediaId);
item.put("author", "");
item.put("title", title);
item.put("content",
"<p style=\"text-align: center; color:white; background:red; width:100%; border-radius: 5px; margin-top:10px\">" +
"<a style=\"align-content: center; color:white; background:red; width:100%;font-size: 25px\" " +
"data-miniprogram-appid =\"wxe5bea3e3b49ee0d5\" " +
"data-miniprogram-path=\"pages/index/index?shareId=\"+shareId+\"&itemCode=\"+itemCode " +
"href= \"\">" +
"点这里购买同款商品" +
"</a>" +
"</p>");
item.put("digest", remark);
item.put("content_source_url", "点击阅读原文后跳转的链接地址");
item.put("need_open_comment", 1);
item.put("only_fans_can_comment", 1);
item.put("show_cover_pic", 1);
articles.add(item);
Articles.put("articles", articles);
String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + WX_MP_ACCESS_TOKEN, Articles.toJSONString());
String media_id = JSONObject.parseObject(result).getString("media_id");
System.out.println("图文id" + media_id);
return media_id;
}
注意!! 这里的articles格式一定不能错 !!
{
"articles": [
{
"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p",
"author":"xxx",
"title":"Happy Day",
"content_source_url":"www.qq.com",
"content":"content",
"digest":"digest",
"show_cover_pic":1,
"need_open_comment":1,
"only_fans_can_comment":1
},
{
"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p",
"author":"xxx",
"title":"Happy Day",
"content_source_url":"www.qq.com",
"content":"content",
"digest":"digest",
"show_cover_pic":0,
"need_open_comment":1,
"only_fans_can_comment":1
}
]
}
第四步、获取关注公众号的用户openId
注意这里的openId不是小程序用户的openId也不是微信的openId,这里的openId是用户与该公众号绑定的openId 是相对于该公众号而言的.
access_token: 公众号access_token
next_openid: 不传的话返回的是最后该值是最后一位关注公众号的用户openId
请求地址:https://api.weixin.qq.com/cgi-bin/user/get
/*** 获取关注公众号的用户列表OpenIds**/
public String getMemberOpenIdByWxMp() {
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("access_token", WX_MP_ACCESS_TOKEN);
paramMap.put("next_openid", "");
String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/user/get", paramMap);
String data = JSONObject.parseObject(result).getString("data");
JSONObject dataObj = JSONObject.parseObject(data);
String openIds = dataObj.getString("openid");
System.out.println("获取到用户列表" + openIds);
return openIds;
}
第五步、群发消息
(uids为Array格式)
media_id: 图文消息的id (第三步获取)
tourse: 用户ids
mpnews: 类型
send_ignore_reprint: 是否允许转发
请求地址: https://api.weixin.qq.com/cgi-bin/message/mass/send
/** 群发消息 (预览,向单个用发送消息)**/
public String sendMessageToMembers(String mediaId) {
JSONArray uIds = JSONArray.parseArray(getMemberOpenIdByWxMp());//uids为Array格式
JSONObject end = new JSONObject();
JSONObject mpnews = new JSONObject();
mpnews.put("media_id", mediaId);
end.put("touser", uIds);
end.put("mpnews", mpnews);
end.put("msgtype", "mpnews");
end.put("send_ignore_reprint", 1);
String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + WX_MP_ACCESS_TOKEN, end.toJSONString());
System.out.println("群发结果" + result);
return result;
}
群发图文消息格式:
(更多类型的消息去看文档吧…)
{
"touser":[
"OPENID1",
"OPENID2"
],
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews",
"send_ignore_reprint":0
}