querylist采集微信公众号文章( 微信小程序中如何打开公众号中的文章(图))
优采云 发布时间: 2021-11-13 14:08querylist采集微信公众号文章(
微信小程序中如何打开公众号中的文章(图))
微信小程序获取文章公众号列表,显示文章示例代码
更新时间:2020年3月10日16:08:20 作者:YO_RUI
本文文章主要介绍微信小程序获取公众号列表文章以及显示文章的示例代码。文章中介绍的示例代码非常详细,对大家的学习或者工作都有一定的借鉴作用。参考学习的价值,有需要的朋友,和小编一起学习吧。
如何在微信小程序中打开公众号中的文章,步骤比较简单。
1、公众号设置
如果小程序想要获取公众号的资料,公众号需要做一些设置。
1.1个绑定小程序
公众号需要绑定目标小程序,否则无法开通公众号文章。
在公众号管理界面,点击小程序管理--> 关联小程序
输入小程序的AppID进行搜索绑定。
1.2 公众号开发者功能配置
(1)在公众号管理界面,点击开发模块中的基本配置选项。
(2) 打开开发者密文(AppSecret),注意保存并修改密文。
(3) 设置ip白名单,这个是发起请求的机器的外网ip,如果是在自己的电脑上,就是自己电脑的外网ip。如果部署到server,是服务器的外网ip。
2、获取文章信息的步骤
以下仅作为演示。
实际项目中,在自己的服务器程序中获取,不要直接在小程序中获取,毕竟需要用到appid、appsecret等高机密参数。
2.1 获取 access_token
access_token是公众号全局唯一的接口调用凭证,公众号在调用各个接口时需要使用access_token。API 文档
private String getToken() throws MalformedURLException, IOException, ProtocolException {
// access_token接口https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
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));
}
System.out.println(sb.toString());
in.close();
return sb.toString();
}
2.2 获取文章的列表
API 文档
private String getContentList(String token) throws IOException {
String path = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token;
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("content-type", "application/json;charset=utf-8");
connection.connect();
// post发送的参数
Map map = new HashMap();
map.put("type", "news"); // news表示图文类型的素材,具体看API文档
map.put("offset", 0);
map.put("count", 1);
// 将map转换成json字符串
String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson
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();
return sb.toString();
}
测试:
@Test
public void test() throws IOException {
String result1 = getToken();
Map token = (Map) JSON.parseObject(result1);
String result2 = getContentList(token.get("access_token").toString());
System.out.println(result2);
}
转换成json格式,参数说明见上面API文档
第二张图中的url为公众号文章的地址。将有与获得的 tem 项目数量一样多的项目。只要得到以上结果,在小程序中打开公众号文章已经成功过半。
最后在小程序中使用组件打开,src为文章的url地址。
本文介绍微信小程序获取公众号文章列表并展示文章文章的示例代码,以及更多获取公众号的相关小程序文章列表内容请搜索脚本之家之前的文章或继续浏览下方的相关文章。希望大家以后多多支持Script Home!