微信公众号开发之新增永久图文素材(十)
<h3 id="上传图文消息内的图片获取URL"><span style="font-family: 黑体"><strong>一、上传图文消息内的图片获取URL</strong></span></h3><p><span style="font-family: 黑体">本接口所上传的图片不占用公众号的素材库中图片数量的100000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。</span></p>
<p><span style="font-family: 黑体"><strong>接口调用请求说明</strong></span></p>
<p><span style="font-family: 黑体">http请求方式: POST,https协议 https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN 调用示例(使用curl命令,用FORM表单方式上传一个图片): curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN"</span></p>
<p><span style="font-family: 黑体"><strong>参数说明</strong></span></p>
<table class=" cke_show_border">
<thead>
<tr><th><span style="font-family: 黑体">参数</span></th><th><span style="font-family: 黑体">是否必须</span></th><th><span style="font-family: 黑体">说明</span></th></tr>
</thead>
<tbody>
<tr>
<td><span style="font-family: 黑体">access_token</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">调用接口凭证</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">media</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">form-data中媒体文件标识,有filename、filelength、content-type等信息</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: 黑体"><strong>返回说明</strong> 正常情况下的返回结果为:</span></p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="14">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;url\&quot;:\&quot;http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0\&quot;\n\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><span style="font-family: 黑体"><code class="hljs">{
"url":"http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0"
}
</code></span></pre>
<span class="cke_reset cke_widget_drag_handler_container" style="font-family: 黑体"><img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p><span style="font-family: 黑体">其中url就是上传图片的URL,可放置图文消息中使用。</span></p>
<p><span style="font-family: 黑体">新建一个NewsUtil类,在这里创建一个上传图文素材图片的方法</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="13"><img class="has cke_widget_element lazyload" alt="" width="1200" height="855" src="https://img-blog.csdnimg.cn/20200109155046429.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109155046429.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;855&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109155046429.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">/**<br> 上传图文消息内的图片获取URL<br> */<br> public String uploadimg(String filePath) {<br><br> String accessToken = accessTokenUtil.getAccessToken();<br> if (accessToken != null) {<br> String url = URIConstant.UPLOAD_IMG_URL.replace("ACCESS_TOKEN", accessToken);<br> log.info("UPLOAD_IMG_URL:{}",url);<br><br> //设置请求体,注意是LinkedMultiValueMap<br> MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();<br><br> //设置上传文件<br> FileSystemResource fileSystemResource = new FileSystemResource(filePath);<br> data.add("media", fileSystemResource);<br><br> //上传文件,设置请求头<br> HttpHeaders httpHeaders = new HttpHeaders();<br> httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);<br> httpHeaders.setContentLength(fileSystemResource.getFile().length());<br><br> HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data,<br> httpHeaders);<br> try{<br> //这里RestTemplate请求返回的字符串直接转换成JSONObject会报异常,后续深入找一下原因<br>// ResponseEntity<JSONObject> resultEntity = restTemplate.exchange(url,<br>// HttpMethod.POST, requestEntity, JSONObject.class);<br> String resultJSON = restTemplate.postForObject(url, requestEntity, String.class);<br> log.info("上传返回的信息是:{}",resultJSON);<br> return resultJSON;<br> }catch (Exception e){<br> log.error(e.getMessage());<br> }<br> }<br> return null;<br><br> }</code></span></pre>
<p><span style="font-family: 黑体">照例,我们在swagger中新建一个方法测试一下我们的方法</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="12"><img class="has cke_widget_element lazyload" alt="" width="1200" height="710" src="https://img-blog.csdnimg.cn/20200109155216345.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109155216345.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;710&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109155216345.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">@ApiOperation(value = "上传图文消息内的图片获取URL")<br>@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)<br>public Object uploadImg(String filePath) {<br><br> String result = newsUtil.uploadimg(filePath);<br> log.info("resut:{}",JSONObject.parseObject(result).toJSONString());<br> return result;<br>}</code></span></pre>
<p><span style="font-family: 黑体">在swagger中提交请求</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="11"><img class="has cke_widget_element lazyload" alt="" width="1200" height="797" src="https://img-blog.csdnimg.cn/20200109155335596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109155335596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;797&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109155335596.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p><span style="font-family: 黑体"> <span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="10"><img class="has cke_widget_element lazyload" alt="" width="1200" height="695" src="https://img-blog.csdnimg.cn/20200109155436465.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109155436465.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;695&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109155436465.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></span></p>
<h3><span style="font-family: 黑体"><strong> 二、新增永久图文素材</strong></span></h3>
<p><span style="font-family: 黑体"><strong>接口调用请求说明</strong></span></p>
<p><span style="font-family: 黑体">http请求方式: POST,https协议 https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN</span></p>
<p><span style="font-family: 黑体"><strong>调用示例</strong></span></p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="9">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;articles\&quot;: [{\n \&quot;title\&quot;: TITLE,\n \&quot;thumb_media_id\&quot;: THUMB_MEDIA_ID,\n \&quot;author\&quot;: AUTHOR,\n \&quot;digest\&quot;: DIGEST,\n \&quot;show_cover_pic\&quot;: SHOW_COVER_PIC(0 / 1),\n \&quot;content\&quot;: CONTENT,\n \&quot;content_source_url\&quot;: CONTENT_SOURCE_URL,\n \&quot;need_open_comment\&quot;:1,\n \&quot;only_fans_can_comment\&quot;:1\n},\n //若新增的是多图文素材,则此处应还有几段articles结构\n]\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><span style="font-family: 黑体"><code class="hljs">{
"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,
"need_open_comment":1,
"only_fans_can_comment":1
},
//若新增的是多图文素材,则此处应还有几段articles结构
]
}
</code></span></pre>
<span class="cke_reset cke_widget_drag_handler_container" style="font-family: 黑体"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p><span style="font-family: 黑体"><strong>参数说明</strong></span></p>
<table class=" cke_show_border">
<thead>
<tr><th><span style="font-family: 黑体">参数</span></th><th><span style="font-family: 黑体">是否必须</span></th><th><span style="font-family: 黑体">说明</span></th></tr>
</thead>
<tbody>
<tr>
<td><span style="font-family: 黑体">title</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">标题</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">thumb_media_id</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">图文消息的封面图片素材id(必须是永久mediaID)</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">author</span></td>
<td><span style="font-family: 黑体">否</span></td>
<td><span style="font-family: 黑体">作者</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">digest</span></td>
<td><span style="font-family: 黑体">否</span></td>
<td><span style="font-family: 黑体">图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">show_cover_pic</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">是否显示封面,0为false,即不显示,1为true,即显示</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">content</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 "上传图文消息内的图片获取URL"接口获取。外部图片url将被过滤。</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">content_source_url</span></td>
<td><span style="font-family: 黑体">是</span></td>
<td><span style="font-family: 黑体">图文消息的原文地址,即点击“阅读原文”后的URL</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">need_open_comment</span></td>
<td><span style="font-family: 黑体">否</span></td>
<td><span style="font-family: 黑体">Uint32 是否打开评论,0不打开,1打开</span></td>
</tr>
<tr>
<td><span style="font-family: 黑体">only_fans_can_comment</span></td>
<td><span style="font-family: 黑体">否</span></td>
<td><span style="font-family: 黑体">Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论</span></td>
</tr>
</tbody>
</table>
<p><span style="font-family: 黑体"><strong>返回说明</strong></span></p>
<div class="cke_widget_wrapper cke_widget_block cke_widget_codeSnippet cke_widget_wrapper_has cke_widget_selected" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="代码段" data-cke-widget-id="8">
<pre class="has cke_widget_element" data-cke-widget-data="{&quot;code&quot;:&quot;{\n \&quot;media_id\&quot;:MEDIA_ID\n}\n&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet"><span style="font-family: 黑体"><code class="hljs">{
"media_id":MEDIA_ID
}
</code></span></pre>
<span class="cke_reset cke_widget_drag_handler_container" style="font-family: 黑体"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="></span></div>
<p><span style="font-family: 黑体">返回的即为新增的图文消息素材的media_id。</span></p>
<p><span style="font-family: 黑体">我们新建一个NewsUtil类,里面主要是两个方法,一个是组件我们的图文发送数据字符串,一个是把这个数据字符串发送给我们的微信服务器接口</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="7"><img class="has cke_widget_element lazyload" alt="" width="1200" height="864" src="https://img-blog.csdnimg.cn/2020010916171671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/2020010916171671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;864&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/2020010916171671.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">private Articles createArticles(){<br><br> Articles articles = new Articles();<br><br> List<News> dataList = new ArrayList<>();<br> Newsnews1 = new News();<br> news1.setTitle("标题");<br> news1.setThumb_media_id("J49eq_VE823b_wZH3Op4DFkLa4Lm4jkTSxX_VbiBWhY");<br> news1.setAuthor("作者");<br> news1.setDigest("图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。");<br> news1.setShow_cover_pic(1);//显示封面<br> news1.setContent("图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 \"上传图文消息内的图片获取URL\"接口获取。外部图片url将被过滤。");<br> news1.setContent_source_url("https://www.baidu.com/");//图文消息的原文地址,即点击“阅读原文”后的URL<br> news1.setNeed_open_comment(1); //Uint32是否打开评论,0不打开,1打开<br> news1.setOnly_fans_can_comment(1); //Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论<br><br><br> Newsnews2 = new News();<br> news2.setTitle("标题");<br> news2.setThumb_media_id("J49eq_VE823b_wZH3Op4DOvK45tuhPJfr3n1_h1w1h8");<br> news2.setAuthor("作者");<br> news2.setDigest("图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。");<br> news2.setShow_cover_pic(1);//显示封面<br> news2.setContent("图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 \"上传图文消息内的图片获取URL\"接口获取。外部图片url将被过滤。");<br> news2.setContent_source_url("https://www.baidu.com/");//图文消息的原文地址,即点击“阅读原文”后的URL<br> news2.setNeed_open_comment(1); //Uint32是否打开评论,0不打开,1打开<br> news2.setOnly_fans_can_comment(1); //Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论<br> dataList.add(news1);<br> dataList.add(news2);<br><br> articles.setArticles(dataList);<br> return articles;<br><br>}<br>/**<br> *新增永久图文素材<br> */<br>public String addNews() {<br><br> Articles articles = this.createArticles();<br> String accessToken = accessTokenUtil.getAccessToken();<br> if (accessToken != null) {<br> log.info("URL{}", URIConstant.ADD_NEWS_URL);<br> String url = URIConstant.ADD_NEWS_URL.replace("ACCESS_TOKEN", accessToken);<br> log.info("ADD_NEWS_URL:{}", url);<br><br> //将菜单对象转换成JSON字符串<br> String jsonNews = JSONObject.toJSONString(articles);<br> log.info("JSONNEWS:{}",jsonNews);<br><br> //发起POST请求创建菜单<br> String jsonObject = restTemplate.postForObject(url, jsonNews,String.class);<br><br> return jsonObject;<br> }<br> return null;<br>}</code></span></pre>
<p><span style="font-family: 黑体">显然这里我们定义了两个POJO,News.java和Articles.java</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="6"><img class="has cke_widget_element lazyload" alt="" width="1200" height="781" src="https://img-blog.csdnimg.cn/20200109161828135.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109161828135.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;781&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109161828135.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">package com.xu.wemall.pojo.news;<br><br>import io.swagger.annotations.ApiModel;<br>import io.swagger.annotations.ApiModelProperty;<br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.NoArgsConstructor;<br><br>@Data<br>@NoArgsConstructor<br>@AllArgsConstructor<br>@ApiModel(value = "新闻消息发送对象")<br>public class News {<br><br> @ApiModelProperty(value = "标题")<br> private String title;<br><br> @ApiModelProperty(value = "图文消息的封面图片素材id(必须是永久 media_ID)")<br> private String thumb_media_id;<br><br> @ApiModelProperty(value = "作者")<br> private String author;<br><br> @ApiModelProperty(value = "图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空")<br> private String digest;<br><br> @ApiModelProperty(value = "是否显示封面,0为false,即不显示,1为true,即显示")<br> private Integer show_cover_pic;<br><br> @ApiModelProperty(value = "图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS")<br> private String content;<br><br> @ApiModelProperty(value = "图文消息的原文地址,即点击“阅读原文”后的URL")<br> private String content_source_url;<br><br> @ApiModelProperty(value = "是否打开评论,0不打开,1打开")<br> private Integer need_open_comment;<br><br> @ApiModelProperty(value = "是否粉丝才可评论,0所有人可评论,1粉丝才可评论")<br> private Integer only_fans_can_comment;<br><br>}<br></code></span></pre>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="5"><img class="has cke_widget_element lazyload" alt="" width="1200" height="600" src="https://img-blog.csdnimg.cn/20200109161927602.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109161927602.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;600&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109161927602.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">package com.xu.wemall.pojo.news;<br><br>import lombok.AllArgsConstructor;<br>import lombok.Data;<br>import lombok.EqualsAndHashCode;<br>import lombok.NoArgsConstructor;<br><br>import java.util.List;<br><br>@Data<br>@AllArgsConstructor<br>@NoArgsConstructor<br>@EqualsAndHashCode(callSuper = false)<br>public class Articles {<br><br> List<News> articles;<br><br>}<br><br></code></span></pre>
<p><span style="font-family: 黑体"><strong>这里的的每个News里需要注意哪些属性是必填项,当需要thumb_media_id这个参数时候,请通过【获取永久素材列表】接口先获取</strong></span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="4"><img class="has cke_widget_element lazyload" alt="" width="1200" height="773" src="https://img-blog.csdnimg.cn/20200109162228917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109162228917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;773&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109162228917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">/**<br> * 获取素菜列表<br> * @return<br> */<br>public String batchgetMaterial(String type, Integer offset, Integer count){<br><br> String accessToken = accessTokenUtil.getAccessToken();<br> if(accessToken != null){<br> String url = URIConstant.BATCHGET_MATERIAL_URL.replace("ACCESS_TOKEN", accessToken);<br> log.info("BATCHGET_MATERIAL_URL:{}",url);<br><br> JSONObject jsonObject = new JSONObject();<br> //素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)<br> jsonObject.put("type", type);<br> //从全部素材的该偏移位置开始返回,0表示从第一个素材 返回<br> jsonObject.put("offset", offset);<br> //返回素材的数量,取值在1到20之间<br> jsonObject.put("count", count);<br><br> //发起POST请求<br> String resultString = restTemplate.postForObject(url, jsonObject.toJSONString(),String.class);<br> return resultString;<br> }<br> return null;<br>}</code></span></pre>
<p><span style="font-family: 黑体">我们在swagger里请求这个接口,获取到我们的永久素材的media_id</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="3"><img class="has cke_widget_element lazyload" alt="" width="1200" height="767" src="https://img-blog.csdnimg.cn/20200109162350857.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109162350857.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;767&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109162350857.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p><span style="font-family: 黑体">最后我们在NewsController中写一个方法,提交我们的图文素材,测试我们的代码</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="2"><img class="has cke_widget_element lazyload" alt="" width="1200" height="652" src="https://img-blog.csdnimg.cn/20200109162544874.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109162544874.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1200&quot;,&quot;height&quot;:&quot;652&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109162544874.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<pre><span style="font-family: 黑体"><code class="language-html hljs">@ApiOperation(value = "上传图文素材")<br>@RequestMapping(value = "/addNews", method = RequestMethod.POST)<br>public Object addNews() throws Exception{<br><br> String result = newsUtil.addNews();<br> //log.info("resut:{}",JSONObject.parseObject(result).toJSONString());<br> return result;<br>}</code></span></pre>
<p><span style="font-family: 黑体">最后在swagger中执行我们的提交图文素材的方法,提交成功并成功获取到我们的图文素材的media_id</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="1"><img class="has cke_widget_element lazyload" alt="" width="1112" height="877" src="https://img-blog.csdnimg.cn/20200109162728625.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/20200109162728625.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1112&quot;,&quot;height&quot;:&quot;877&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/20200109162728625.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p><span style="font-family: 黑体">到这里,我们成功上传了我们的永久图文素材,不放心的话我们可以调用我们的getMaterialcount【获取素菜数量】看看上传后的数据统计</span></p>
<p><span class="cke_widget_wrapper cke_widget_inline cke_widget_image cke_image_nocaption cke_widget_selected" style="font-family: 黑体" data-cke-widget-wrapper="1" data-cke-filter="off" data-cke-display-name="图像" data-cke-widget-id="0"><img class="has cke_widget_element lazyload" alt="" width="1103" height="599" src="https://img-blog.csdnimg.cn/2020010916322242.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70" data-cke-widget-data="{&quot;hasCaption&quot;:false,&quot;src&quot;:&quot;https://img-blog.csdnimg.cn/2020010916322242.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70&quot;,&quot;alt&quot;:&quot;&quot;,&quot;width&quot;:&quot;1103&quot;,&quot;height&quot;:&quot;599&quot;,&quot;lock&quot;:true,&quot;align&quot;:&quot;none&quot;,&quot;classes&quot;:{&quot;has&quot;:1}}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="image" data-src="https://img-blog.csdnimg.cn/2020010916322242.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTM1MjEyMjA=,size_16,color_FFFFFF,t_70"><span class="cke_reset cke_widget_drag_handler_container"><img class="cke_reset cke_widget_drag_handler lazyload" title="点击并拖拽以移动" alt="" width="15" height="15" data-cke-widget-drag-handler="1" data-src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw=="><span class="cke_image_resizer" title="点击并拖拽以改变尺寸"></span></span></span></p>
<p><span style="font-family: 黑体">好了,我们下回再见,拜拜!</span></p>
<p> </p>
<h3>如果您觉得此文有帮助,可以小小打赏一下,持续更新更有动力哟!</h3>
<p> </p>
<p><img alt="" width="218" height="274" data-src="https://img2020.cnblogs.com/blog/1501920/202004/1501920-20200402133129576-79870337.png"><img src="https://img2020.cnblogs.com/blog/1501920/202004/1501920-20200402133221455-2112921293.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/xulijun137/p/12213652.html
頁:
[1]