洛塔服务号回复017获取代码。
公众号后台设置IP白名单。
前面有些地方其实已经调用这个,为了得到media_id。
请求参数中type是媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)。
以图片类型为例:
// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();System.out.println(result);String accessToken = JSON.parseObject(result).getString("access_token");// 新增临时素材url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + accessToken + "&type=image";File file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"type":"image","media_id":"1pd6iWTtkWfMd-BZX9I3a4Tm6kHiJlPhbzFKNMUyQUArfAKlKAXaVEwUOK-3LsMe","created_at":1670570665,"item":[]}System.out.println(result);
返回数据样例:
{
"type":"image",
"media_id":"1pd6iWTtkWfMd-BZX9I3a4Tm6kHiJlPhbzFKNMUyQUArfAKlKAXaVEwUOK-3LsMe",
"created_at":1670570665,
"item":[ ]
}
获取临时素材分为视频类和非视频类,获取到的格式不一样。如果是非视频类,直接接收到素材文件,本地保存使用即可。如果是视频类的,返回的是视频的url,本地可以对这个url进行继续操作。
视频类的比较简单,下面介绍下非视频类获取到文件后进行保存。
// 获取临时素材(非视频类)String savePath = "D:/11.jpg"; //保存下来的文件路径和名称String mediaId = JSON.parseObject(result).getString("media_id");url = " https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + accessToken + "&media_id=" + mediaId;Response response = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute();if(response.statusCode() == 200) {BufferedInputStream bufferedInputStream = response.bodyStream();byte[] buffer = new byte[1024];int len = 0;FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);while ((len = bufferedInputStream.read(buffer, 0, 1024)) != -1) {bufferedOut.write(buffer, 0, len);}bufferedOut.flush();bufferedOut.close();System.out.println("保存完成:" + savePath);}// 如果是视频文件,用下面的方式
// result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute().body();
// // 视频返回格式:{ "video_url":DOWN_URL }
// System.out.println(result);
这里会得到一个图片的url,用来插入到图文消息内。 直接使用外部的图片url是不能成功在素材内显示的。
// 上传图文消息内的图片获取URL。 这个url只能腾讯系域名内使用,其他地方用会被屏蔽url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + accessToken;file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0"}System.out.println(result);
返回的就是一个url,样例:
{
"url":"http://mmbiz.qpic.cn//mmbiz_png//B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q//0"
}
新增永久素材分为视频类和其他类。如果是视频类,需要指定描述信息,包括视频标题和视频介绍,而诸如图片类则不需要上传这些。
非视频类新增永久素材样例:
// 新增永久素材(非视频)url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=image";file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"media_id":"6XvVazVT3F4qsVQ-QW23lhD2JFBCpOayJVKEUvR2T24XVLvTSVTYjbfT038ErTyW","url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0?wx_fmt=png","item":[]}System.out.println(result);
视频类新增永久素材
// 新增永久素材(视频)param = new JSONObject();param.put("title", "视频标题");param.put("introduction", "视频介绍");url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=video";file = new File("D:/1.mp4");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).data("description", param.toString()).timeout(60000).execute().body();// {"media_id":"6XvVazVT3F4qsVQ-QW23lqhl-X1EKmke5-oeZM969ckNHqNnfngWltCamzmXP-PW","url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0?wx_fmt=png","item":[]}System.out.println(result);
非视频类的永久素材获取到的是文件,可以直接保存;如果是视频类的,得到的是视频的url,可以本地对此url进行操作。
非视频类获取永久素材样例:
// 获取永久素材(非视频)savePath = "D:/111.jpg"; //保存下来的文件路径和名称mediaId = JSON.parseObject(result).getString("media_id");url = " https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + accessToken;JSONObject param = new JSONObject();param.put("media_id", mediaId);response = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute();if(response.statusCode() == 200) {BufferedInputStream bufferedInputStream = response.bodyStream();byte[] buffer = new byte[1024];int len = 0;FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);while ((len = bufferedInputStream.read(buffer, 0, 1024)) != -1) {bufferedOut.write(buffer, 0, len);}bufferedOut.flush();bufferedOut.close();System.out.println("保存完成:" + savePath);}
视频类获取永久素材样例:
// 获取永久素材(视频)savePath = "D:/222.mp4"; //保存下来的文件路径和名称mediaId = JSON.parseObject(result).getString("media_id");param = new JSONObject();param.put("media_id", mediaId);url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"title":"视频标题","description":"","down_url":"http:\/\/203.205.136.93\/vweixinp.tc.qq.com\/1007_5e87b6d9116d4cb6b6f950f9f112e0da.f10.mp4?vkey=0DCBE63134C59AEF366ABD98224872C7AB274D10F3E261786B1FBC32218428CCC988FE097350FA214526F1671AF7E20C6CDE776EA209D01EFBA9635BED58CA095BECE78045E1FAC2DAD94B1CCDB35D5068FAB4FC0E83BA81&sha=0&save=1","newcat":"","newsubcat":"","tags":[],"cover_url":"","vid":"1007_5e87b6d9116d4cb6b6f950f9f112e0da"}System.out.println(result);
这个就比较简单了,直接上样例:
// 删除永久素材param = new JSONObject();param.put("media_id", mediaId);url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"errcode":0,"errmsg":"ok"}System.out.println(result);
获取到的总数有四种
voice_count语音总数量
video_count视频总数量
image_count图片总数量
news_count图文总数量
// 获取素材总数url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute().body();// {"voice_count":0,"video_count":5,"image_count":33,"news_count":0}System.out.println(result);
需要根据素材类型进行请求,每次最多获取20条。
// 获取素材列表param = new JSONObject();param.put("type", "video"); //素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)param.put("offset", "0"); //从全部素材的该偏移位置开始返回,0表示从第一个素材 返回param.put("count", "20"); //返回素材的数量,取值在1到20之间url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"item":[{"media_id":"6XvVazVT3F4qsVQ-QW23ll5Hl3mPk-BZvPZ6q9nUWnL4_E0m9JXdri6nCfTYZ3HG","name":"视频标题","update_time":1670575041,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23llrmr7VBAtByYbUdOmuDF1S7FPEwVWuctCZ8QsO94NqP","name":"视频标题","update_time":1670574857,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23loB2h52KHMEeNva8B-HQ6j3-geb2yzADAr83hXqbkqVt","name":"视频标题","update_time":1670574820,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23lqiC159B12i-Iy_1404_AFhsEcWsuvxlKC1GLsYrKk0c","name":"视频标题","update_time":1670574735,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23lhZjulUCZMHWn5eRyL69G8spweQ-CEfvW57HyiFA8c2y","name":"视频","update_time":1663818592,"cover_url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/B2O8ZzVDiaBLDFUvZQZr8Mpp3iaIzNrO6aQ9tkPgibOQqwFtTMbglDywRiaSf4HPqa5ibwjhGVFMHZMtxYmdaLcYHyQ\/0?wx_fmt=jpeg","description":"","newcat":"搞笑","newsubcat":"其他","tags":[],"vid":"wxv_2587487411262275585"}],"total_count":5,"item_count":5}System.out.println(result);
返回数据样例:
{ "item":[ { "media_id":"6XvVazVT3F4qsVQ-QW23ll5Hl3mPk-BZvPZ6q9nUWnL4_E0m9JXdri6nCfTYZ3HG", "name":"视频标题", "update_time":1670575041, "description":"", "tags":[ ] }, { "media_id":"6XvVazVT3F4qsVQ-QW23llrmr7VBAtByYbUdOmuDF1S7FPEwVWuctCZ8QsO94NqP", "name":"视频标题", "update_time":1670574857, "description":"", "tags":[ ] }, { "media_id":"6XvVazVT3F4qsVQ-QW23loB2h52KHMEeNva8B-HQ6j3-geb2yzADAr83hXqbkqVt", "name":"视频标题", "update_time":1670574820, "description":"", "tags":[ ] }, { "media_id":"6XvVazVT3F4qsVQ-QW23lqiC159B12i-Iy_1404_AFhsEcWsuvxlKC1GLsYrKk0c", "name":"视频标题", "update_time":1670574735, "description":"", "tags":[ ] }, { "media_id":"6XvVazVT3F4qsVQ-QW23lhZjulUCZMHWn5eRyL69G8spweQ-CEfvW57HyiFA8c2y", "name":"视频", "update_time":1663818592, "cover_url":"http://mmbiz.qpic.cn/mmbiz_jpg//B2O8ZzVDiaBLDFUvZQZr8Mpp3iaIzNrO6aQ9tkPgibOQqwFtTMbglDywRiaSf4HPqa5ibwjhGVFMHZMtxYmdaLcYHyQ/0?wx_fmt=jpeg", "description":"", "newcat":"搞笑", "newsubcat":"其他", "tags":[ ], "vid":"wxv_2587487411262275585" } ], "total_count":5, "item_count":5
}
package com.lootaa.wechat;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;/*** 素材管理* 前置条件:公众号后台设置ip白名单*/
public class Test017 {public static final String APPID = "wx276049d6a7551dca";public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";/*** 完整项目源码可关注公众号"lootaayun"(洛塔),回复017获取*/public static void main(String[] args) throws Exception {// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();System.out.println(result);String accessToken = JSON.parseObject(result).getString("access_token");// 新增临时素材url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + accessToken + "&type=image";File file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"type":"image","media_id":"1pd6iWTtkWfMd-BZX9I3a4Tm6kHiJlPhbzFKNMUyQUArfAKlKAXaVEwUOK-3LsMe","created_at":1670570665,"item":[]}System.out.println(result);// 获取临时素材(非视频类)String savePath = "D:/11.jpg"; //保存下来的文件路径和名称String mediaId = JSON.parseObject(result).getString("media_id");url = " https://api.weixin.qq.com/cgi-bin/media/get?access_token=" + accessToken + "&media_id=" + mediaId;Response response = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute();if(response.statusCode() == 200) {BufferedInputStream bufferedInputStream = response.bodyStream();byte[] buffer = new byte[1024];int len = 0;FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);while ((len = bufferedInputStream.read(buffer, 0, 1024)) != -1) {bufferedOut.write(buffer, 0, len);}bufferedOut.flush();bufferedOut.close();System.out.println("保存完成:" + savePath);}// 如果是视频文件,用下面的方式
// result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute().body();
// // 视频返回格式:{ "video_url":DOWN_URL }
// System.out.println(result);// 上传图文消息内的图片获取URL。 这个url只能腾讯系域名内使用,其他地方用会被屏蔽url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + accessToken;file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0"}System.out.println(result);// 新增永久素材(非视频)url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=image";file = new File("D:/1.jpg");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).timeout(60000).execute().body();// {"media_id":"6XvVazVT3F4qsVQ-QW23lhD2JFBCpOayJVKEUvR2T24XVLvTSVTYjbfT038ErTyW","url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0?wx_fmt=png","item":[]}System.out.println(result);// 获取永久素材(非视频)savePath = "D:/111.jpg"; //保存下来的文件路径和名称mediaId = JSON.parseObject(result).getString("media_id");url = " https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + accessToken;JSONObject param = new JSONObject();param.put("media_id", mediaId);response = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute();if(response.statusCode() == 200) {BufferedInputStream bufferedInputStream = response.bodyStream();byte[] buffer = new byte[1024];int len = 0;FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);while ((len = bufferedInputStream.read(buffer, 0, 1024)) != -1) {bufferedOut.write(buffer, 0, len);}bufferedOut.flush();bufferedOut.close();System.out.println("保存完成:" + savePath);}// 新增永久素材(视频)param = new JSONObject();param.put("title", "视频标题");param.put("introduction", "视频介绍");url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + accessToken + "&type=video";file = new File("D:/1.mp4");result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).data("media", file.getName(), new FileInputStream(file)).data("description", param.toString()).timeout(60000).execute().body();// {"media_id":"6XvVazVT3F4qsVQ-QW23lqhl-X1EKmke5-oeZM969ckNHqNnfngWltCamzmXP-PW","url":"http:\/\/mmbiz.qpic.cn\/mmbiz_png\/B2O8ZzVDiaBJb8ia9UylXQZHtyBfH88hmLbdGsV343SFUdD1I04jCxsMQ0lLgJrUNj0Htrx9fUHrjMvB9fKgaV5Q\/0?wx_fmt=png","item":[]}System.out.println(result);// 获取永久素材(视频)savePath = "D:/222.mp4"; //保存下来的文件路径和名称mediaId = JSON.parseObject(result).getString("media_id");param = new JSONObject();param.put("media_id", mediaId);url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"title":"视频标题","description":"","down_url":"http:\/\/203.205.136.93\/vweixinp.tc.qq.com\/1007_5e87b6d9116d4cb6b6f950f9f112e0da.f10.mp4?vkey=0DCBE63134C59AEF366ABD98224872C7AB274D10F3E261786B1FBC32218428CCC988FE097350FA214526F1671AF7E20C6CDE776EA209D01EFBA9635BED58CA095BECE78045E1FAC2DAD94B1CCDB35D5068FAB4FC0E83BA81&sha=0&save=1","newcat":"","newsubcat":"","tags":[],"cover_url":"","vid":"1007_5e87b6d9116d4cb6b6f950f9f112e0da"}System.out.println(result);// 删除永久素材param = new JSONObject();param.put("media_id", mediaId);url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"errcode":0,"errmsg":"ok"}System.out.println(result);// 获取素材总数url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.GET).execute().body();// {"voice_count":0,"video_count":5,"image_count":33,"news_count":0}System.out.println(result);// 获取素材列表param = new JSONObject();param.put("type", "video"); //素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)param.put("offset", "0"); //从全部素材的该偏移位置开始返回,0表示从第一个素材 返回param.put("count", "20"); //返回素材的数量,取值在1到20之间url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + accessToken;result = Jsoup.connect(url).ignoreContentType(true).maxBodySize(0).method(Method.POST).requestBody(param.toString()).execute().body();// {"item":[{"media_id":"6XvVazVT3F4qsVQ-QW23ll5Hl3mPk-BZvPZ6q9nUWnL4_E0m9JXdri6nCfTYZ3HG","name":"视频标题","update_time":1670575041,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23llrmr7VBAtByYbUdOmuDF1S7FPEwVWuctCZ8QsO94NqP","name":"视频标题","update_time":1670574857,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23loB2h52KHMEeNva8B-HQ6j3-geb2yzADAr83hXqbkqVt","name":"视频标题","update_time":1670574820,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23lqiC159B12i-Iy_1404_AFhsEcWsuvxlKC1GLsYrKk0c","name":"视频标题","update_time":1670574735,"description":"","tags":[]},{"media_id":"6XvVazVT3F4qsVQ-QW23lhZjulUCZMHWn5eRyL69G8spweQ-CEfvW57HyiFA8c2y","name":"视频","update_time":1663818592,"cover_url":"http:\/\/mmbiz.qpic.cn\/mmbiz_jpg\/B2O8ZzVDiaBLDFUvZQZr8Mpp3iaIzNrO6aQ9tkPgibOQqwFtTMbglDywRiaSf4HPqa5ibwjhGVFMHZMtxYmdaLcYHyQ\/0?wx_fmt=jpeg","description":"","newcat":"搞笑","newsubcat":"其他","tags":[],"vid":"wxv_2587487411262275585"}],"total_count":5,"item_count":5}System.out.println(result);}}