进行调用上传永久图片素材接口(https://api.weixin.qq.com/cgi-bin/material/add_material?type=image),能成功上传,返回的MediaId作为参数,
放到下方接口中,进行新建草稿箱操作,报40007,麻烦各位大佬帮忙看一下是什么问题呗,谢谢啦
【请求地址】: https://api.weixin.qq.com/cgi-bin/draft/add?access_token=58_zkWMzJas5vQ5jryj-打码处理-AJAYNC
【请求参数】:{"articles":[{"showCoverPic":false,"thumbMediaId":"jOhIk28IJd3Ic8USAcnm-打码处理-T7uXJGZ3aAfUG53OUPjRsepT_7brMUUQovoR399Y","author":"safdsdf","digest":"sdfsadf","contentSourceUrl":"","title":"sadfasd","content":"<p>asdfdsaf<\/p>"}]}
报错信息:
错误代码:40007, 错误信息:不合法的媒体文件 id,微信原始报文:{"errcode":40007,"errmsg":"invalid media_id hint: [CYVXsa08238236] rid: 62b03bd7-6ebfdab5-0360c303"}
原代码:
@Override
public void uploadNews(YxArticleDto wxNewsArticleItem) throws WxErrorException {
// 获取公众号操作对象
WxMpService wxMpService = WxMpConfiguration.getWxMpService();
// 创建原创文章对象
WxMpMaterialNews wxMpMaterialNews = new WxMpMaterialNews();
WxMpNewsArticle article = new WxMpNewsArticle();
// 1、上传文章中要用到的图片至素材库中
WxMpMaterialUploadResult wxMpMaterialUploadResult = this.uploadPhotoToWx(wxMpService,
wxNewsArticleItem.getImageInput());
// 设置封面图片在素材库中的 media_id(媒体id)
wxNewsArticleItem.setThumbMediaId(wxMpMaterialUploadResult.getMediaId());
String content = this.processContent(wxMpService, wxNewsArticleItem.getContent());
//处理content,主要是内容里面的媒体信息(图片、视频等)需要上传至素材库中
article.setTitle(wxNewsArticleItem.getTitle());// 文章标题
article.setShowCoverPic(false);// 正文中是否显示封面
article.setThumbMediaId(wxMpMaterialUploadResult.getMediaId());// 封面图片在素材库中的 media_id(媒体id)
article.setAuthor(wxNewsArticleItem.getAuthor());// 文章作者
article.setDigest(wxNewsArticleItem.getSynopsis());// 图文消息摘要,单图文消息才有,如果不设置,就默认取文章正文前 64 个字,多图文消息此处为空
article.setContent(content);// 文章内容
article.setContentSourceUrl(wxNewsArticleItem.getUrl());// 文章中有一个阅读原文按钮,点击阅读原文后的跳转地址
// article.setNeedOpenComment( wxNewsArticleItem );// 是否打开评论-测试号无权限
// article.setOnlyFansCanComment( wxNewsArticleItem );// 是否粉丝才可评论-测试号无权限
wxMpMaterialNews.addArticle(article);
String jsonStr = JSONUtil.toJsonStr(wxMpMaterialNews);
// String jsonStr = JSON.toJSONString(jo);
log.info("wxMpMaterialNews : {}", jsonStr);
// 图文消息发送提交草稿箱
String post = wxMpService.post(WxMpApiUrl.Draft.ADD_DRAFT, jsonStr);
//开始群发图文消息
WxMpMassTagMessage massMessage = new WxMpMassTagMessage();
massMessage.setMsgType(WxConsts.MassMsgType.MPNEWS);// 群发的消息类型
massMessage.setSendAll(true);// 是否发送全部对象
// massMessage.setTagId(new Long(100));// 要发送的用户组 id,因为前面指定的是全部发送,所以该字段可以不设置
WxMpMassSendResult massResult = wxMpService.getMassMessageService()
.massGroupMessageSend(massMessage);
if (!massResult.getErrorCode().equals("0")) {
log.info("error:" + massResult.getErrorMsg());
throw new ErrorRequestException("发送失败");
}
log.info("massResult : {}", JSONUtil.toJsonStr(massResult));
}
/**
* 上传素材到公众号素材库中
*
* @param wxMpService
* @param uploadFile 需要上传的文件
* @return
* @throws WxErrorException
*/
private WxMpMaterialUploadResult uploadPhotoToWx(WxMpService wxMpService, String uploadFile) throws WxErrorException {
WxMpMaterial wxMpMaterial = new WxMpMaterial();
WxMpMaterialUploadResult wxMpMaterialUploadResult = null;
// 创建一个存储地址
String filename = System.currentTimeMillis() + ".png";
String aa = "/";
String[] split = uploadFile.split(aa);
// /home/cqjl/css-system-server/file/ + 文件名
String tempFilePath = uploadDirStr + split[split.length-1];
log.info("tempFilePath : {}", tempFilePath);
log.info("uploadFile : {}", uploadFile);
// 下载图片至临时位置// localFileUrl + tempFilePath
HttpUtil.downloadFile(fileUrl + uploadFile, cn.hutool.core.io.FileUtil.file(tempFilePath));
// 上传图片至永久素材中
File picFile = new File(tempFilePath);
wxMpMaterial.setFile(picFile);
wxMpMaterial.setName(picFile.getName());
log.info("picFile name : {}", picFile.getName());
try {
// 上传前需要采用我们配置中的URl,正式环境需要访问代理服务器
String uploadUrl = String.format(MATERIAL_ADD_URL.getUrl(wxMpService.getWxMpConfigStorage()), WxConsts.MediaFileType.IMAGE);
log.info("picFile uploadUrl : {}", uploadUrl);
// 封面图片不能通过图文消息内图片上传的接口去上传,只能通过上传其他类型的素材接口去上传(指定媒体类型为图片),会被上传至默认分组
wxMpMaterialUploadResult = wxMpService.execute(MaterialUploadRequestExecutor.create(wxMpService.getRequestHttp()), uploadUrl, wxMpMaterial);
// wxMpMaterialUploadResult = wxMpService.getMaterialService().materialFileUpload(WxConsts.MediaFileType.IMAGE, wxMpMaterial);
log.info("wxMpMaterialUploadResult : {}", JSONUtil.toJsonStr(wxMpMaterialUploadResult));
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (picFile.exists()){
picFile.delete();
log.info("picFile exists : {}", picFile.exists());
}
}
return wxMpMaterialUploadResult;
}
是否因为获取永久素材只能返回图文和视频????????????????
也遇到这个问题