Development of new micro-channel public number of permanent graphic material (X)

Pictures within a uploaded graphic message gets URL

This interface to upload images do not occupy 100,000 limited number of public Library in number of images. Pictures only supports jpg / png format and size must be below 1MB.

Interface call requesting explanation

http request method: POST, https protocol https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN call sample (using curl command, upload a picture with FORM Form mode): curl -F [email protected] "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN"

Parameter Description

parameter Do you have to Explanation
access_token Yes Call Interface credentials
media Yes form-data in the media file identification, there filename, filelength, content-type information

Returning to the description  returns results under normal circumstances are:

{
    "url":  "http://mmbiz.qpic.cn/mmbiz/gLO17UPS6FS2xsypf378iaNhWacZ1G1UplZYWEYfwvuU6Ont96b1roYs CNFwaRrSaKTPCUdBK9DgEHicsKwWCBRQ/0"

}

Where url is uploaded images URL, graphic messages can be placed in use.

Create a NewsUtil class, created here a method to upload pictures of graphic material

/**
上传图文消息内的图片获取URL
*/
public String uploadimg(String filePath) {

String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
String url = URIConstant.UPLOAD_IMG_URL.replace("ACCESS_TOKEN", accessToken);
log.info("UPLOAD_IMG_URL:{}",url);

//设置请求体,注意是LinkedMultiValueMap
MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();

//设置上传文件
FileSystemResource fileSystemResource = new FileSystemResource(filePath);
data.add("media", fileSystemResource);

//上传文件,设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
httpHeaders.setContentLength(fileSystemResource.getFile().length());

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data,
httpHeaders);
try{
//这里RestTemplate请求返回的字符串直接转换成JSONObject会报异常,后续深入找一下原因
// ResponseEntity<JSONObject> resultEntity = restTemplate.exchange(url,
// HttpMethod.POST, requestEntity, JSONObject.class);
String resultJSON = restTemplate.postForObject(url, requestEntity, String.class);
log.info("上传返回的信息是:{}",resultJSON);
return resultJSON;
}catch (Exception e){
log.error(e.getMessage());
}
}
return null;

}

As usual, we are in a new swagger in a way to test our approach

@ApiOperation(value = "上传图文消息内的图片获取URL")
@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
public Object uploadImg(String filePath) {

String result = newsUtil.uploadimg(filePath);
log.info("resut:{}",JSONObject.parseObject(result).toJSONString());
return result;
}

Submit a request swagger in

 

 Second, the new permanent graphic material

Interface call requesting explanation

http request method: POST, https protocol https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN

Example calls

{
    "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结构
]
}

Parameter Description

parameter Do you have to Explanation
title Yes title
thumb_media_id Yes Cover picture material graphic message id (must be a permanent mediaID)
author no Author
digest no Abstract graphic messages, have only a single graphic message digest, and more graphic here is empty. If this field is not filled, the default text crawl first 64 words.
show_cover_pic Yes Whether to display the covers, 0 is false, that is not displayed, 1 is true, is displayed
content Yes Graphic details of the message, HTML tags support, must be less than 20,000 characters, less than 1M, and here will remove JS, sources involved in the picture url must "upload pictures in the graphic message retrieval URL" interface to obtain. External images url will be filtered out.
content_source_url Yes Original Address graphic message that the URL after clicking "Read original"
need_open_comment no Uint32 whether to open a review, 0 is not open, open 1
only_fans_can_comment no Whether fans before Uint32 comment 0 comments owner may, before 1 Fans Comments

Returning to the description

{
   "media_id":MEDIA_ID
}

media_id returned is the new graphic news clips.

NewsUtil we create a new class, which is mainly two methods, is a component of our teletext transmission data string, a character string is to send the data to a server interface our microcells

private Articles createArticles(){

Articles articles = new Articles();

List<News> dataList = new ArrayList<>();
News news1 = new News();
news1.setTitle("标题");
news1.setThumb_media_id("J49eq_VE823b_wZH3Op4DFkLa4Lm4jkTSxX_VbiBWhY");
news1.setAuthor("作者");
news1.setDigest("图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。");
news1.setShow_cover_pic(1);//显示封面
news1.setContent("图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 \"上传图文消息内的图片获取URL\"接口获取。外部图片url将被过滤。");
news1.setContent_source_url("https://www.baidu.com/"); //图文消息的原文地址,即点击“阅读原文”后的URL
news1.setNeed_open_comment(1); //Uint32 是否打开评论,0不打开,1打开
news1.setOnly_fans_can_comment(1); //Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论


News news2 = new News();
news2.setTitle("标题");
news2.setThumb_media_id("J49eq_VE823b_wZH3Op4DOvK45tuhPJfr3n1_h1w1h8");
news2.setAuthor("作者");
news2.setDigest("图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空。如果本字段为没有填写,则默认抓取正文前64个字。");
news2.setShow_cover_pic(1);//显示封面
news2.setContent("图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS,涉及图片url必须来源 \"上传图文消息内的图片获取URL\"接口获取。外部图片url将被过滤。");
news2.setContent_source_url("https://www.baidu.com/"); //图文消息的原文地址,即点击“阅读原文”后的URL
news2.setNeed_open_comment(1); //Uint32 是否打开评论,0不打开,1打开
news2.setOnly_fans_can_comment(1); //Uint32 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
dataList.add(news1);
dataList.add(news2);

articles.setArticles(dataList);
return articles;

}
/**
*新增永久图文素材
*/
public String addNews() {

Articles articles = this.createArticles();
String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
log.info("URL{}", URIConstant.ADD_NEWS_URL);
String url = URIConstant.ADD_NEWS_URL.replace("ACCESS_TOKEN", accessToken);
log.info("ADD_NEWS_URL:{}", url);

//将菜单对象转换成JSON字符串
String jsonNews = JSONObject.toJSONString(articles);
log.info("JSONNEWS:{}",jsonNews);

//发起POST请求创建菜单
String jsonObject = restTemplate.postForObject(url, jsonNews,String.class);

return jsonObject;
}
return null;
}

Obviously here we define two POJO, News.java and Articles.java

package com.xu.wemall.pojo.news;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "新闻消息发送对象")
public class News {

@ApiModelProperty(value = "标题")
private String title;

@ApiModelProperty(value = "图文消息的封面图片素材id(必须是永久 media_ID)")
private String thumb_media_id;

@ApiModelProperty(value = "作者")
private String author;

@ApiModelProperty(value = "图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空")
private String digest;

@ApiModelProperty(value = "是否显示封面,0为false,即不显示,1为true,即显示")
private Integer show_cover_pic;

@ApiModelProperty(value = "图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS")
private String content;

@ApiModelProperty(value = "图文消息的原文地址,即点击“阅读原文”后的URL")
private String content_source_url;

@ApiModelProperty(value = "是否打开评论,0不打开,1打开")
private Integer need_open_comment;

@ApiModelProperty(value = "是否粉丝才可评论,0所有人可评论,1粉丝才可评论")
private Integer only_fans_can_comment;

}

package com.xu.wemall.pojo.news;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Articles {

List<News> articles;

}

Each News Here's need to pay attention to what attributes are required, when required thumb_media_id this parameter, please obtain permanent material through the [first obtain a list of Interface

/**
* 获取素菜列表
* @return
*/
public String batchgetMaterial(String type, Integer offset, Integer count){

String accessToken = accessTokenUtil.getAccessToken();
if(accessToken != null){
String url = URIConstant.BATCHGET_MATERIAL_URL.replace("ACCESS_TOKEN", accessToken);
log.info("BATCHGET_MATERIAL_URL:{}",url);

JSONObject jsonObject = new JSONObject();
//素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
jsonObject.put("type", type);
//从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
jsonObject.put("offset", offset);
//返回素材的数量,取值在1到20之间
jsonObject.put("count", count);

//发起POST请求
String resultString = restTemplate.postForObject(url, jsonObject.toJSONString(),String.class);
return resultString;
}
return null;
}

We ask this interface in the swagger, the media_id get to our permanent clip

Finally, we write a method in NewsController, the author of the graphic material we test our code

@ApiOperation(value = "上传图文素材")
@RequestMapping(value = "/addNews", method = RequestMethod.POST)
public Object addNews() throws Exception{

String result = newsUtil.addNews();
//log.info("resut:{}",JSONObject.parseObject(result).toJSONString());
return result;
}

Finally, the implementation of our method to submit graphic material in the swagger, the author successfully and successfully get to our graphic material of media_id

Here, we successfully uploaded our permanent graphic material, do not worry if we can call our getMaterialcount [get] to see the number of vegetarian statistics after uploading

Well, next time we bye, bye!

Guess you like

Origin www.cnblogs.com/xulijun137/p/12213652.html