• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JAVA 获取微信公众号发布的文章列表内容

武飞扬头像
萌新萌老师
帮助1

文章目录


代码业务场景

最近在给客户开发一款小程序,然后客户还有自己运营的公众号,想要把公众号里面发布的一些内容能够同步到小程序里面进行展示。如下所示,获取公众号里面的发表记录→发布→发表成功的文章内容,删除的内容是获取不到的。

学新通

一、介绍

开始翻了下微信公众号的开发文档,其实文档里面些的很清楚,怎么访问,怎么获取,怎么解析写的一清二处,不清楚的同学可以看下链接:微信开放平台公众号开发文档
看了下网上说的一些还要公众号绑定小程序,其实如果只是单纯的获取公众号里面的文章信息的话,是不需要绑定的,如果要在小程序里面打开公众号返回的文章url的话,才需要绑定。

二、开始操作

1.获取公众号的开发者id(AppID)和开发者密码(AppSecret),以及设置IP白名单

登录微信开放平台,然后用公众号的账号扫描进入,在基本设置里面设置开发者密码和服务器访问的白名单ip,多个ip用回车隔开。

 学新通

 如本地调试不知道自己本地外网IP的话,可以先不设置,后面debug报错的提示信息里面会有你的ip

如果需要绑定小程序的话,可以点击左侧的小程序管理,没有操作过的话,右边会显示开通。

我自己点击开通点了五六次,等了十分钟才显示出来,不知道是微信的问题还是本地的网络问题。

开通之后,会有添加,填入自己的小程序AppID,即可关联

学新通

2.代码操作

  1.  
    package com.ruoyi.common.core.domain.entity.miniprogram;
  2.  
     
  3.  
    import java.util.List;
  4.  
     
  5.  
    public class OfficialAccountVo {
  6.  
    private Integer pageIndex;
  7.  
     
  8.  
    private Integer pageSize;
  9.  
     
  10.  
    private Integer totalPage;
  11.  
     
  12.  
    private List<OfficialAccount> objectList;
  13.  
     
  14.  
    public Integer getPageIndex() {
  15.  
    return pageIndex;
  16.  
    }
  17.  
     
  18.  
    public void setPageIndex(Integer pageIndex) {
  19.  
    this.pageIndex = pageIndex;
  20.  
    }
  21.  
     
  22.  
    public Integer getPageSize() {
  23.  
    return pageSize;
  24.  
    }
  25.  
     
  26.  
    public void setPageSize(Integer pageSize) {
  27.  
    this.pageSize = pageSize;
  28.  
    }
  29.  
     
  30.  
    public Integer getTotalPage() {
  31.  
    return totalPage;
  32.  
    }
  33.  
     
  34.  
    public void setTotalPage(Integer totalPage) {
  35.  
    this.totalPage = totalPage;
  36.  
    }
  37.  
     
  38.  
    public List<OfficialAccount> getObjectList() {
  39.  
    return objectList;
  40.  
    }
  41.  
     
  42.  
    public void setObjectList(List<OfficialAccount> objectList) {
  43.  
    this.objectList = objectList;
  44.  
    }
  45.  
    }
学新通
  1.  
    package com.ruoyi.common.core.domain.entity.miniprogram;
  2.  
     
  3.  
    public class OfficialAccount {
  4.  
    private String title;
  5.  
     
  6.  
    private String url;
  7.  
     
  8.  
    private String thumbUrl;
  9.  
     
  10.  
    private String thumbMedialId;
  11.  
     
  12.  
    private String isDelete;
  13.  
     
  14.  
    private String author;
  15.  
     
  16.  
    private String digest;
  17.  
     
  18.  
    private String content;
  19.  
     
  20.  
    private String onlyFansCanComment;
  21.  
     
  22.  
    private String showOverPic;
  23.  
     
  24.  
    private String contentSourceUrl;
  25.  
     
  26.  
    private String needOpenComment;
  27.  
     
  28.  
    public String getTitle() {
  29.  
    return title;
  30.  
    }
  31.  
     
  32.  
    public void setTitle(String title) {
  33.  
    this.title = title;
  34.  
    }
  35.  
     
  36.  
    public String getUrl() {
  37.  
    return url;
  38.  
    }
  39.  
     
  40.  
    public void setUrl(String url) {
  41.  
    this.url = url;
  42.  
    }
  43.  
     
  44.  
    public String getThumbUrl() {
  45.  
    return thumbUrl;
  46.  
    }
  47.  
     
  48.  
    public void setThumbUrl(String thumbUrl) {
  49.  
    this.thumbUrl = thumbUrl;
  50.  
    }
  51.  
     
  52.  
    public String getThumbMedialId() {
  53.  
    return thumbMedialId;
  54.  
    }
  55.  
     
  56.  
    public void setThumbMedialId(String thumbMedialId) {
  57.  
    this.thumbMedialId = thumbMedialId;
  58.  
    }
  59.  
     
  60.  
    public String getIsDelete() {
  61.  
    return isDelete;
  62.  
    }
  63.  
     
  64.  
    public void setIsDelete(String isDelete) {
  65.  
    this.isDelete = isDelete;
  66.  
    }
  67.  
     
  68.  
    public String getAuthor() {
  69.  
    return author;
  70.  
    }
  71.  
     
  72.  
    public void setAuthor(String author) {
  73.  
    this.author = author;
  74.  
    }
  75.  
     
  76.  
    public String getDigest() {
  77.  
    return digest;
  78.  
    }
  79.  
     
  80.  
    public void setDigest(String digest) {
  81.  
    this.digest = digest;
  82.  
    }
  83.  
     
  84.  
    public String getContent() {
  85.  
    return content;
  86.  
    }
  87.  
     
  88.  
    public void setContent(String content) {
  89.  
    this.content = content;
  90.  
    }
  91.  
     
  92.  
    public String getOnlyFansCanComment() {
  93.  
    return onlyFansCanComment;
  94.  
    }
  95.  
     
  96.  
    public void setOnlyFansCanComment(String onlyFansCanComment) {
  97.  
    this.onlyFansCanComment = onlyFansCanComment;
  98.  
    }
  99.  
     
  100.  
    public String getShowOverPic() {
  101.  
    return showOverPic;
  102.  
    }
  103.  
     
  104.  
    public void setShowOverPic(String showOverPic) {
  105.  
    this.showOverPic = showOverPic;
  106.  
    }
  107.  
     
  108.  
    public String getContentSourceUrl() {
  109.  
    return contentSourceUrl;
  110.  
    }
  111.  
     
  112.  
    public void setContentSourceUrl(String contentSourceUrl) {
  113.  
    this.contentSourceUrl = contentSourceUrl;
  114.  
    }
  115.  
     
  116.  
    public String getNeedOpenComment() {
  117.  
    return needOpenComment;
  118.  
    }
  119.  
     
  120.  
    public void setNeedOpenComment(String needOpenComment) {
  121.  
    this.needOpenComment = needOpenComment;
  122.  
    }
  123.  
    }
学新通
  1.  
    import com.alibaba.fastjson.JSON;
  2.  
    import com.alibaba.fastjson.JSONArray;
  3.  
    import com.alibaba.fastjson.JSONObject;
  4.  
    import com.ruoyi.common.core.domain.AjaxResult;
  5.  
    import com.ruoyi.common.core.domain.entity.miniprogram.OfficialAccount;
  6.  
    import com.ruoyi.common.core.domain.entity.miniprogram.OfficialAccountVo;
  7.  
    import org.springframework.web.bind.annotation.*;
  8.  
     
  9.  
    import java.io.*;
  10.  
    import java.net.HttpURLConnection;
  11.  
    import java.net.MalformedURLException;
  12.  
    import java.net.ProtocolException;
  13.  
    import java.net.URL;
  14.  
    import java.util.*;
  15.  
     
  16.  
    @RestController
  17.  
    @RequestMapping("/miniApp/officialAccount")
  18.  
    public class OfficialAccountController {
  19.  
    /**
  20.  
    * 获取公众号发布文章列表
  21.  
    * @param officialAccountVo
  22.  
    * @return
  23.  
    * @throws IOException
  24.  
    */
  25.  
    @ResponseBody
  26.  
    @PostMapping(value = "/getContentList")
  27.  
    private AjaxResult getContentList(@RequestBody OfficialAccountVo officialAccountVo) throws IOException {
  28.  
    String result1 = getWxAppToken();
  29.  
    Map<String, Object> token1 = (Map<String, Object>) JSON.parseObject(result1);
  30.  
    // String path = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" token1.get("access_token").toString(); --获取素材
  31.  
    String path = "https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=" token1.get("access_token").toString();
  32.  
    //模拟http请求
  33.  
    URL url = new URL(path);
  34.  
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  35.  
    connection.setRequestMethod("POST");
  36.  
    connection.setDoOutput(true);
  37.  
    connection.setRequestProperty("content-type", "application/json");
  38.  
    connection.connect();
  39.  
    // post发送的参数
  40.  
    Map<String, Object> map = new HashMap<>();
  41.  
    map.put("offset", (officialAccountVo.getPageIndex()-1)* officialAccountVo.getPageSize()); //分页内容起始index
  42.  
    map.put("count", officialAccountVo.getPageSize()); //显示内容数量
  43.  
    map.put("no_content", 0); //1 表示不返回 content 字段,0 表示正常返回,默认为 0
  44.  
    // 将map转换成json字符串
  45.  
    String paramBody = JSON.toJSONString(map);
  46.  
    OutputStream out = connection.getOutputStream();
  47.  
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
  48.  
    bw.write(paramBody); // 向流中写入参数字符串
  49.  
    bw.flush();
  50.  
    InputStream in = connection.getInputStream();
  51.  
    byte[] b = new byte[100];
  52.  
    int len = -1;
  53.  
    StringBuffer sb = new StringBuffer();
  54.  
    while ((len = in.read(b)) != -1) {
  55.  
    sb.append(new String(b, 0, len));
  56.  
    }
  57.  
    in.close();
  58.  
    JSONObject json = JSONObject.parseObject(sb.toString());
  59.  
    //以上是已经获取到文章列表,下面是视业务场景进行json操作,如不需要则直接返回sb.toString()。
  60.  
    //取出json中的item
  61.  
    String item = json.getString("item");
  62.  
    //查看返回的总数
  63.  
    String total = json.getString("total_count");
  64.  
    officialAccountVo.setTotalPage(Integer.valueOf(total));
  65.  
    List<OfficialAccount> arrayList = new ArrayList<>();
  66.  
    //如果返回的列表总数为0就没必要解析了
  67.  
    if(Integer.valueOf(total)>0) {
  68.  
    //item为数组json类型,这时需要转换成JSONArray
  69.  
    JSONArray jsonArray = JSONObject.parseArray(item);
  70.  
    int size = jsonArray.size();
  71.  
    List<String> contentList = new ArrayList<>();
  72.  
    for (int i = 0; i < size; i ) {
  73.  
    JSONObject jsonObject = jsonArray.getJSONObject(i);
  74.  
    String content = jsonObject.getString("content");
  75.  
    contentList.add(content);
  76.  
    }
  77.  
    //解析文章内容
  78.  
    contentList.forEach(data -> {
  79.  
    //content为文章模块
  80.  
    JSONObject jsonObject = JSON.parseObject(data);
  81.  
    //取出文章列表信息并转成json
  82.  
    String news = jsonObject.getString("news_item");
  83.  
    JSONArray jsonArray1 = JSONObject.parseArray(news);
  84.  
    //循环数组json
  85.  
    for (int i = 0; i < jsonArray1.size(); i ) {
  86.  
    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
  87.  
    OfficialAccount jsonEntity = new OfficialAccount();
  88.  
    jsonEntity.setThumbUrl(jsonObject1.getString("thumb_url"));
  89.  
    jsonEntity.setThumbMedialId(jsonObject1.getString("thumb_media_id"));
  90.  
    jsonEntity.setIsDelete(jsonObject1.getString("is_deleted"));
  91.  
    jsonEntity.setAuthor(jsonObject1.getString("author"));
  92.  
    jsonEntity.setOnlyFansCanComment(jsonObject1.getString("only_fans_can_comment"));
  93.  
    jsonEntity.setDigest(jsonObject1.getString("digest"));
  94.  
    jsonEntity.setShowOverPic(jsonObject1.getString("show_cover_pic"));
  95.  
    jsonEntity.setContentSourceUrl(jsonObject1.getString("content_source_url"));
  96.  
    jsonEntity.setNeedOpenComment(jsonObject1.getString("need_open_comment"));
  97.  
    jsonEntity.setTitle(jsonObject1.getString("title"));
  98.  
    jsonEntity.setContent(jsonObject1.getString("content"));
  99.  
    jsonEntity.setUrl(jsonObject1.getString("url"));
  100.  
    arrayList.add(jsonEntity);
  101.  
    }
  102.  
    });
  103.  
    }
  104.  
    officialAccountVo.setObjectList(arrayList);
  105.  
    return AjaxResult.success(officialAccountVo);
  106.  
    }
  107.  
     
  108.  
     
  109.  
    /**
  110.  
    * 获取公众号token
  111.  
    * @return
  112.  
    * @throws MalformedURLException
  113.  
    * @throws IOException
  114.  
    * @throws ProtocolException
  115.  
    */
  116.  
    private String getWxAppToken() throws MalformedURLException, IOException, ProtocolException {
  117.  
    String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
  118.  
    String appid = "*******"; //公众号的开发者ID(AppID)
  119.  
    String secret = "*******"; //公众号的开发者密码(AppSecret)
  120.  
    URL url = new URL(path "&appid=" appid "&secret=" secret);
  121.  
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  122.  
    connection.setRequestMethod("GET");
  123.  
    connection.connect();
  124.  
    InputStream in = connection.getInputStream();
  125.  
    byte[] b = new byte[100];
  126.  
    int len = -1;
  127.  
    StringBuffer sb = new StringBuffer();
  128.  
    while ((len = in.read(b)) != -1) {
  129.  
    sb.append(new String(b, 0, len));
  130.  
    }
  131.  
    in.close();
  132.  
    return sb.toString();
  133.  
    }
  134.  
    }
学新通

上面最后一段代码里面需要讲自己公众号的AppIDAppSecret替换进去;

不知道自己本地外网ip的话,可以在这里打个断点,会提示你的ip信息不在白名单访问名单里面,然后去公众号平台里面添加一下就可以本地测试了

学新通

 本地测试结果:

学新通


总结

  代码也比较简单,参考开发文档直接写就好了,有不对的地方还希望各位多多指教。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfikgg
系列文章
更多 icon
同类精品
更多 icon
继续加载