JAVA 获取微信公众号发布的文章列表内容
文章目录
代码业务场景
最近在给客户开发一款小程序,然后客户还有自己运营的公众号,想要把公众号里面发布的一些内容能够同步到小程序里面进行展示。如下所示,获取公众号里面的发表记录→发布→发表成功的文章内容,删除的内容是获取不到的。
一、介绍
开始翻了下微信公众号的开发文档,其实文档里面些的很清楚,怎么访问,怎么获取,怎么解析写的一清二处,不清楚的同学可以看下链接:微信开放平台公众号开发文档
看了下网上说的一些还要公众号绑定小程序,其实如果只是单纯的获取公众号里面的文章信息的话,是不需要绑定的,如果要在小程序里面打开公众号返回的文章url的话,才需要绑定。
二、开始操作
1.获取公众号的开发者id(AppID)和开发者密码(AppSecret),以及设置IP白名单
登录微信开放平台,然后用公众号的账号扫描进入,在基本设置里面设置开发者密码和服务器访问的白名单ip,多个ip用回车隔开。
如本地调试不知道自己本地外网IP的话,可以先不设置,后面debug报错的提示信息里面会有你的ip
如果需要绑定小程序的话,可以点击左侧的小程序管理,没有操作过的话,右边会显示开通。
我自己点击开通点了五六次,等了十分钟才显示出来,不知道是微信的问题还是本地的网络问题。
开通之后,会有添加,填入自己的小程序AppID,即可关联
2.代码操作
-
package com.ruoyi.common.core.domain.entity.miniprogram;
-
-
import java.util.List;
-
-
public class OfficialAccountVo {
-
private Integer pageIndex;
-
-
private Integer pageSize;
-
-
private Integer totalPage;
-
-
private List<OfficialAccount> objectList;
-
-
public Integer getPageIndex() {
-
return pageIndex;
-
}
-
-
public void setPageIndex(Integer pageIndex) {
-
this.pageIndex = pageIndex;
-
}
-
-
public Integer getPageSize() {
-
return pageSize;
-
}
-
-
public void setPageSize(Integer pageSize) {
-
this.pageSize = pageSize;
-
}
-
-
public Integer getTotalPage() {
-
return totalPage;
-
}
-
-
public void setTotalPage(Integer totalPage) {
-
this.totalPage = totalPage;
-
}
-
-
public List<OfficialAccount> getObjectList() {
-
return objectList;
-
}
-
-
public void setObjectList(List<OfficialAccount> objectList) {
-
this.objectList = objectList;
-
}
-
}
-
package com.ruoyi.common.core.domain.entity.miniprogram;
-
-
public class OfficialAccount {
-
private String title;
-
-
private String url;
-
-
private String thumbUrl;
-
-
private String thumbMedialId;
-
-
private String isDelete;
-
-
private String author;
-
-
private String digest;
-
-
private String content;
-
-
private String onlyFansCanComment;
-
-
private String showOverPic;
-
-
private String contentSourceUrl;
-
-
private String needOpenComment;
-
-
public String getTitle() {
-
return title;
-
}
-
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
-
public String getUrl() {
-
return url;
-
}
-
-
public void setUrl(String url) {
-
this.url = url;
-
}
-
-
public String getThumbUrl() {
-
return thumbUrl;
-
}
-
-
public void setThumbUrl(String thumbUrl) {
-
this.thumbUrl = thumbUrl;
-
}
-
-
public String getThumbMedialId() {
-
return thumbMedialId;
-
}
-
-
public void setThumbMedialId(String thumbMedialId) {
-
this.thumbMedialId = thumbMedialId;
-
}
-
-
public String getIsDelete() {
-
return isDelete;
-
}
-
-
public void setIsDelete(String isDelete) {
-
this.isDelete = isDelete;
-
}
-
-
public String getAuthor() {
-
return author;
-
}
-
-
public void setAuthor(String author) {
-
this.author = author;
-
}
-
-
public String getDigest() {
-
return digest;
-
}
-
-
public void setDigest(String digest) {
-
this.digest = digest;
-
}
-
-
public String getContent() {
-
return content;
-
}
-
-
public void setContent(String content) {
-
this.content = content;
-
}
-
-
public String getOnlyFansCanComment() {
-
return onlyFansCanComment;
-
}
-
-
public void setOnlyFansCanComment(String onlyFansCanComment) {
-
this.onlyFansCanComment = onlyFansCanComment;
-
}
-
-
public String getShowOverPic() {
-
return showOverPic;
-
}
-
-
public void setShowOverPic(String showOverPic) {
-
this.showOverPic = showOverPic;
-
}
-
-
public String getContentSourceUrl() {
-
return contentSourceUrl;
-
}
-
-
public void setContentSourceUrl(String contentSourceUrl) {
-
this.contentSourceUrl = contentSourceUrl;
-
}
-
-
public String getNeedOpenComment() {
-
return needOpenComment;
-
}
-
-
public void setNeedOpenComment(String needOpenComment) {
-
this.needOpenComment = needOpenComment;
-
}
-
}
-
import com.alibaba.fastjson.JSON;
-
import com.alibaba.fastjson.JSONArray;
-
import com.alibaba.fastjson.JSONObject;
-
import com.ruoyi.common.core.domain.AjaxResult;
-
import com.ruoyi.common.core.domain.entity.miniprogram.OfficialAccount;
-
import com.ruoyi.common.core.domain.entity.miniprogram.OfficialAccountVo;
-
import org.springframework.web.bind.annotation.*;
-
-
import java.io.*;
-
import java.net.HttpURLConnection;
-
import java.net.MalformedURLException;
-
import java.net.ProtocolException;
-
import java.net.URL;
-
import java.util.*;
-
-
-
-
public class OfficialAccountController {
-
/**
-
* 获取公众号发布文章列表
-
* @param officialAccountVo
-
* @return
-
* @throws IOException
-
*/
-
-
-
private AjaxResult getContentList( OfficialAccountVo officialAccountVo)throws IOException {
-
String result1 = getWxAppToken();
-
Map<String, Object> token1 = (Map<String, Object>) JSON.parseObject(result1);
-
// String path = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" token1.get("access_token").toString(); --获取素材
-
String path = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=" token1.get("access_token").toString();
-
//模拟http请求
-
URL url = new URL(path);
-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-
connection.setRequestMethod("POST");
-
connection.setDoOutput(true);
-
connection.setRequestProperty("content-type", "application/json");
-
connection.connect();
-
// post发送的参数
-
Map<String, Object> map = new HashMap<>();
-
map.put("offset", (officialAccountVo.getPageIndex()-1)* officialAccountVo.getPageSize()); //分页内容起始index
-
map.put("count", officialAccountVo.getPageSize()); //显示内容数量
-
map.put("no_content", 0); //1 表示不返回 content 字段,0 表示正常返回,默认为 0
-
// 将map转换成json字符串
-
String paramBody = JSON.toJSONString(map);
-
OutputStream out = connection.getOutputStream();
-
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
-
bw.write(paramBody); // 向流中写入参数字符串
-
bw.flush();
-
InputStream in = connection.getInputStream();
-
byte[] b = new byte[100];
-
int len = -1;
-
StringBuffer sb = new StringBuffer();
-
while ((len = in.read(b)) != -1) {
-
sb.append(new String(b, 0, len));
-
}
-
in.close();
-
JSONObject json = JSONObject.parseObject(sb.toString());
-
//以上是已经获取到文章列表,下面是视业务场景进行json操作,如不需要则直接返回sb.toString()。
-
//取出json中的item
-
String item = json.getString("item");
-
//查看返回的总数
-
String total = json.getString("total_count");
-
officialAccountVo.setTotalPage(Integer.valueOf(total));
-
List<OfficialAccount> arrayList = new ArrayList<>();
-
//如果返回的列表总数为0就没必要解析了
-
if(Integer.valueOf(total)>0) {
-
//item为数组json类型,这时需要转换成JSONArray
-
JSONArray jsonArray = JSONObject.parseArray(item);
-
int size = jsonArray.size();
-
List<String> contentList = new ArrayList<>();
-
for (int i = 0; i < size; i ) {
-
JSONObject jsonObject = jsonArray.getJSONObject(i);
-
String content = jsonObject.getString("content");
-
contentList.add(content);
-
}
-
//解析文章内容
-
contentList.forEach(data -> {
-
//content为文章模块
-
JSONObject jsonObject = JSON.parseObject(data);
-
//取出文章列表信息并转成json
-
String news = jsonObject.getString("news_item");
-
JSONArray jsonArray1 = JSONObject.parseArray(news);
-
//循环数组json
-
for (int i = 0; i < jsonArray1.size(); i ) {
-
JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
-
OfficialAccount jsonEntity = new OfficialAccount();
-
jsonEntity.setThumbUrl(jsonObject1.getString("thumb_url"));
-
jsonEntity.setThumbMedialId(jsonObject1.getString("thumb_media_id"));
-
jsonEntity.setIsDelete(jsonObject1.getString("is_deleted"));
-
jsonEntity.setAuthor(jsonObject1.getString("author"));
-
jsonEntity.setOnlyFansCanComment(jsonObject1.getString("only_fans_can_comment"));
-
jsonEntity.setDigest(jsonObject1.getString("digest"));
-
jsonEntity.setShowOverPic(jsonObject1.getString("show_cover_pic"));
-
jsonEntity.setContentSourceUrl(jsonObject1.getString("content_source_url"));
-
jsonEntity.setNeedOpenComment(jsonObject1.getString("need_open_comment"));
-
jsonEntity.setTitle(jsonObject1.getString("title"));
-
jsonEntity.setContent(jsonObject1.getString("content"));
-
jsonEntity.setUrl(jsonObject1.getString("url"));
-
arrayList.add(jsonEntity);
-
}
-
});
-
}
-
officialAccountVo.setObjectList(arrayList);
-
return AjaxResult.success(officialAccountVo);
-
}
-
-
-
/**
-
* 获取公众号token
-
* @return
-
* @throws MalformedURLException
-
* @throws IOException
-
* @throws ProtocolException
-
*/
-
private String getWxAppToken() throws MalformedURLException, IOException, ProtocolException {
-
String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
-
String appid = "*******"; //公众号的开发者ID(AppID)
-
String secret = "*******"; //公众号的开发者密码(AppSecret)
-
URL url = new URL(path "&appid=" appid "&secret=" secret);
-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-
connection.setRequestMethod("GET");
-
connection.connect();
-
InputStream in = connection.getInputStream();
-
byte[] b = new byte[100];
-
int len = -1;
-
StringBuffer sb = new StringBuffer();
-
while ((len = in.read(b)) != -1) {
-
sb.append(new String(b, 0, len));
-
}
-
in.close();
-
return sb.toString();
-
}
-
}
上面最后一段代码里面需要讲自己公众号的AppID和AppSecret替换进去;
不知道自己本地外网ip的话,可以在这里打个断点,会提示你的ip信息不在白名单访问名单里面,然后去公众号平台里面添加一下就可以本地测试了
本地测试结果:
总结
代码也比较简单,参考开发文档直接写就好了,有不对的地方还希望各位多多指教。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfikgg
系列文章
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13