国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容

這篇具有很好參考價值的文章主要介紹了JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

文章目錄

  • 代碼業(yè)務場景

    一、前提操作

    二、開始操作

    1.獲取公眾號的開發(fā)者id(AppID)和開發(fā)者密碼(AppSecret),以及設置IP白名單

    2.代碼操作

    總結(jié)


代碼業(yè)務場景

最近在給客戶開發(fā)一款小程序,然后客戶還有自己運營的公眾號,想要把公眾號里面發(fā)布的一些內(nèi)容能夠同步到小程序里面進行展示。如下所示,獲取公眾號里面的發(fā)表記錄→發(fā)布→發(fā)表成功的文章內(nèi)容,刪除的內(nèi)容是獲取不到的。

JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容

一、介紹

開始翻了下微信公眾號的開發(fā)文檔,其實文檔里面些的很清楚,怎么訪問,怎么獲取,怎么解析寫的一清二處,不清楚的同學可以看下鏈接:微信開放平臺公眾號開發(fā)文檔
看了下網(wǎng)上說的一些還要公眾號綁定小程序,其實如果只是單純的獲取公眾號里面的文章信息的話,是不需要綁定的,如果要在小程序里面打開公眾號返回的文章url的話,才需要綁定。

二、開始操作

1.獲取公眾號的開發(fā)者id(AppID)和開發(fā)者密碼(AppSecret),以及設置IP白名單

登錄微信開放平臺,然后用公眾號的賬號掃描進入,在基本設置里面設置開發(fā)者密碼和服務器訪問的白名單ip,多個ip用回車隔開。

????JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容

?如本地調(diào)試不知道自己本地外網(wǎng)IP的話,可以先不設置,后面debug報錯的提示信息里面會有你的ip

如果需要綁定小程序的話,可以點擊左側(cè)的小程序管理,沒有操作過的話,右邊會顯示開通。

我自己點擊開通點了五六次,等了十分鐘才顯示出來,不知道是微信的問題還是本地的網(wǎng)絡問題。

開通之后,會有添加,填入自己的小程序AppID,即可關聯(lián)

JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容

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.*;

@RestController
@RequestMapping("/miniApp/officialAccount")
public class OfficialAccountController {
    /**
     * 獲取公眾號發(fā)布文章列表
     * @param officialAccountVo
     * @return
     * @throws IOException
     */
    @ResponseBody
    @PostMapping(value = "/getContentList")
    private AjaxResult getContentList(@RequestBody 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發(fā)送的參數(shù)
        Map<String, Object> map = new HashMap<>();
        map.put("offset", (officialAccountVo.getPageIndex()-1)* officialAccountVo.getPageSize()); //分頁內(nèi)容起始index
        map.put("count", officialAccountVo.getPageSize());  //顯示內(nèi)容數(shù)量
        map.put("no_content", 0);  //1 表示不返回 content 字段,0 表示正常返回,默認為 0
        // 將map轉(zhuǎn)換成json字符串
        String paramBody = JSON.toJSONString(map);
        OutputStream out = connection.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        bw.write(paramBody); // 向流中寫入?yún)?shù)字符串
        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());
        //以上是已經(jīng)獲取到文章列表,下面是視業(yè)務場景進行json操作,如不需要則直接返回sb.toString()。
        //取出json中的item
        String item = json.getString("item");
        //查看返回的總數(shù)
        String total = json.getString("total_count");
        officialAccountVo.setTotalPage(Integer.valueOf(total));
        List<OfficialAccount> arrayList = new ArrayList<>();
        //如果返回的列表總數(shù)為0就沒必要解析了
        if(Integer.valueOf(total)>0) {
            //item為數(shù)組json類型,這時需要轉(zhuǎn)換成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);
            }
           //解析文章內(nèi)容
            contentList.forEach(data -> {
                //content為文章模塊
                JSONObject jsonObject = JSON.parseObject(data);
                //取出文章列表信息并轉(zhuǎn)成json
                String news = jsonObject.getString("news_item");
                JSONArray jsonArray1 = JSONObject.parseArray(news);
                //循環(huán)數(shù)組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 = "*******"; //公眾號的開發(fā)者ID(AppID)
        String secret = "*******"; //公眾號的開發(fā)者密碼(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();
    }
}

上面最后一段代碼里面需要講自己公眾號的AppIDAppSecret替換進去;

不知道自己本地外網(wǎng)ip的話,可以在這里打個斷點,會提示你的ip信息不在白名單訪問名單里面,然后去公眾號平臺里面添加一下就可以本地測試了

JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容

?本地測試結(jié)果:

JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容


總結(jié)

? 代碼也比較簡單,參考開發(fā)文檔直接寫就好了,有不對的地方還希望各位多多指教。文章來源地址http://www.zghlxwxcb.cn/news/detail-490442.html

到了這里,關于JAVA 獲取微信公眾號發(fā)布的文章列表內(nèi)容的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 微信小程序如何跳轉(zhuǎn)到微信公眾號文章,小程序如何關聯(lián)公眾號或訂閱號

    微信小程序如何跳轉(zhuǎn)到微信公眾號文章,小程序如何關聯(lián)公眾號或訂閱號

    公眾號最高管理權(quán)限(或能與最高權(quán)限管理者配合操作) 小程序開發(fā)權(quán)限或最高管理權(quán)限 根據(jù)官方資料描述,小程序中展示微信公眾號中的文章需要使用到 web-view web-view 是一個 web 瀏覽器組件,可以用來承載網(wǎng)頁的容器,會自動鋪滿整個頁面 src:webview 指向網(wǎng)頁的鏈接 特別

    2024年02月14日
    瀏覽(109)
  • SAP ABAP技術文章合集_微信公眾號:ABAP猿

    SAP ABAP技術文章合集_微信公眾號:ABAP猿

    序號 文章標題 01 ABAP OOALV-基本顯示 02 ABAP OOALV-排序、過濾 03 ABAP OOALV-合計、小計 04 ABAP OOALV-選擇模式、觸發(fā)事件 05 ABAP OOALV-隱藏列、修改列標題、添加圖標列、調(diào)整列位置 06 ABAP OOALV-固定列、鼠標懸停文本 07 ABAP OOALV-顏色(列、行、單元格) 08 ABAP OOALV-樣式(列、行、單元格

    2024年02月03日
    瀏覽(53)
  • 詳解織夢首頁、列表頁調(diào)用文章body內(nèi)容的兩種方法

    關于首頁、列表頁調(diào)用文章body內(nèi)容的兩種方法,具體方法如下: 第一種方法: 解析: 1、addrields=”字段1,字段2…” 2、idlist=”文章ID”(可不寫) 3、channelid=”模型ID”,普通文章默認為1(查看方法:核心–頻道模型–內(nèi)容模型管理–id號) 4、function=’cn_substr(html2text(@me)

    2024年02月02日
    瀏覽(21)
  • 微信小程序跳轉(zhuǎn)公眾號文章 web-view方式

    提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔 提示:這里可以添加本文要記錄的大概內(nèi)容: 小程序和公眾號都必須時企業(yè)認證的 1.在小程序里面關聯(lián)相關的公眾號 2.在公眾號里面關聯(lián)相關的小程序 提示:以下是本篇文章正文內(nèi)容,下面案例可供參

    2024年02月09日
    瀏覽(28)
  • php+mysql實現(xiàn)微信公眾號回復關鍵詞新聞列表

    php+mysql實現(xiàn)微信公眾號回復關鍵詞新聞列表

    非常抱歉,我之前理解有誤。如果您想要實現(xiàn)在公眾號發(fā)送,返回新聞列表的功能,可以按照以下步驟進行操作: 1. 創(chuàng)建一個數(shù)據(jù)庫表,用于存儲新聞的標題、鏈接和內(nèi)容等信息。例如,可以創(chuàng)建一個名為news的表,包含id、title、link和content等字段。 2. 在公眾號后臺設

    2023年04月16日
    瀏覽(53)
  • 個人微信公眾號文章留言功能開通方法,無需遷移賬號,三個步驟輕松完成

    個人微信公眾號文章留言功能開通方法,無需遷移賬號,三個步驟輕松完成

    總所周知。微信公眾號關閉了2018年以后開通的微信公眾號的評論管理功能。這樣其實對于個人賬號而言不太方便。現(xiàn)在常見的方法是遷移到別人在18年之前的賬號上去,但是這個操作很麻煩,而且賬號也不一定能弄到,還需要花費大加強。 實際上,公眾號原創(chuàng)圖文是可以插入

    2024年02月06日
    瀏覽(20)
  • 微信公眾平臺(3):微信小程序發(fā)布為什么需要https證書

    微信小程序一定要用https的理由,小程序使用HTTPS鏈接分析 一、HTTPS HTTPS是HTTP的安全版,在 HTTP的基礎上加入SSL證書 (服務器證書)后形成的安全協(xié)議 ,不但可以建立信息加密通過保障數(shù)據(jù)傳輸?shù)陌踩€能 認證服務器的真實性 , 防止“釣魚”網(wǎng)站 。每個微信小程序都需要先

    2024年02月12日
    瀏覽(42)
  • 微信小程序引導關注公眾號(超詳細),獲取公眾號openID,是否關注公眾號信息

    微信小程序引導關注公眾號(超詳細),獲取公眾號openID,是否關注公眾號信息

    需求背景:微信小程序里,需要判斷使用該小程序的用戶是否有關注該小程序關聯(lián)的公眾號,如未關注要引導用戶去關注公眾號(用于公眾號推送信息) 官網(wǎng): https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html 接口(獲取Code): 參數(shù): APPID:公眾號的唯

    2024年02月15日
    瀏覽(42)
  • python爬蟲爬取微信公眾號的閱讀數(shù)、喜愛數(shù)、文章標題和鏈接等信息

    python爬蟲爬取微信公眾號的閱讀數(shù)、喜愛數(shù)、文章標題和鏈接等信息

    爬蟲的步驟: (1)申請自己的公眾號 (2)使用fiddler抓包工具 (3)pycharm (一)申請公眾號 官網(wǎng):微信公眾平臺 填入相關信息創(chuàng)建微信公眾號 進入公眾號界面如下: 找到新的創(chuàng)作-圖文信息 在彈出的界面中查找公眾號文章-輸入公眾號名稱-確定 點擊確認之后,進入公眾號

    2024年02月05日
    瀏覽(26)
  • 帝國cms內(nèi)容頁模板把當前文章發(fā)布時間改成年月號星期幾顯示方式

    把下面代碼加到帝國cms內(nèi)容頁模板最頭部 在帝國cms內(nèi)容頁模板需要顯示的地方加入以下代碼 到此這篇文章就結(jié)束了,帝國cms頁面支持php非常方便拓展。

    2024年02月03日
    瀏覽(91)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包