上一节,我们举例创建了公众平台标签"陕西",之所以讲述微信公众号标签是因为最近有个需求,客户要求根据不同的用户展示不同的公众号菜单,所以去微信公众平台管理中心看了下个性化菜单的接口,其中就有一项,根据关注用户的标签来区分展示菜单。小编会在讲述完标签之后,还会示例利用微信公众平台标签创建个性化菜单。
1. 获取公众号已创建的标签
接口调用请求说明
http请求方式:GET(请使用https协议) https://api.weixin.qq.com/cgi-bin/tags/get?access_token=ACCESS_TOKEN
返回说明
{ "tags":[{ "id":1, "name":"每天一罐可乐星人", "count":0 //此标签下粉丝数 },{ "id":2, "name":"星标组", "count":0 },{ "id":127, "name":"广东", "count":5 } ] }
2. java代码获取标签示例
1),获取access_token
/*** * 获取token V型知识库 www.vxzsk.com * @param appid * @param appSecret * @return */ public static String getToken(String appid,String appSecret){ String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+""; String backData=TestMenu.sendGet(url, "utf-8", 10000); JSONObject jsonObject = JSONObject.fromObject(backData); String access_token=jsonObject.getString("access_token");//调用接口凭证 return access_token; }
说明:appid 和 appSecret分别为应用id和应用秘钥,读者登录自己的公众号管理平台自行查询
TestMenu.sendGet代码
/*** * 模拟get请求 V型知识库 www.vxzsk.com * @param url * @param charset * @param timeout * @return */ public static String sendGet(String url, String charset, int timeout) { String result = ""; try { URL u = new URL(url); try { URLConnection conn = u.openConnection(); conn.connect(); conn.setConnectTimeout(timeout); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String line=""; while ((line = in.readLine()) != null) { result = result + line; } in.close(); } catch (IOException e) { return result; } } catch (MalformedURLException e) { return result; } return result; }
2), 获取公众号已创建的标签示例代码
/**** * 获取标签 * @param access_token * @return */ public static String getTags(String access_token){ String url ="https://api.weixin.qq.com/cgi-bin/tags/get?access_token=";//获取标签接口 String jsonData = TestMenu.sendGet(url+access_token,"utf-8",10000); System.out.println("请求接口返回"+jsonData); return jsonData; }
3. 完整代码示例 main方法 可直接执行(appid和appSecret替换为自己公众号)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; /** * V型知识库 www.vxzsk.com * **/ public class TestMenu { /*** * Post 请求* */ public static String sendPost(String requrl,String param){ URL url; String sTotalString=""; try { url = new URL(requrl); URLConnection connection = url.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "text/xml"); // connection.setRequestProperty("Content-Length", body.getBytes().length+""); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8"); out.write(param); // 向页面传递数据。post的关键所在! out.flush(); out.close(); // 一旦发送成功,用以下方法就可以得到服务器的回应: String sCurrentLine; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream(); // 传说中的三层包装阿! BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream)); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine + "\r\n"; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(sTotalString); return sTotalString; } /*** * 模拟get请求 * @param url * @param charset * @param timeout * @return */ public static String sendGet(String url, String charset, int timeout) { String result = ""; try { URL u = new URL(url); try { URLConnection conn = u.openConnection(); conn.connect(); conn.setConnectTimeout(timeout); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String line=""; while ((line = in.readLine()) != null) { result = result + line; } in.close(); } catch (IOException e) { return result; } } catch (MalformedURLException e) { return result; } return result; } /*** * 获取token * @param appid * @param appSecret * @return */ public static String getToken(String appid,String appSecret){ String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+""; String backData=TestMenu.sendGet(url, "utf-8", 10000); JSONObject jsonObject = JSONObject.fromObject(backData); String access_token=jsonObject.getString("access_token");//调用接口凭证 return access_token; } /*** * 创建标签 * @param access_token * @return */ public static String createTags(String access_token){ //2,创建用户标签 String create_tags_url="https://api.weixin.qq.com/cgi-bin/tags/create?access_token=";//创建标签接口 String params = "{ \"tag\" : { \"name\" : \"陕西\"}}"; String jsonData = TestMenu.sendPost(create_tags_url+access_token, params); System.out.println("请求接口返回"+jsonData); return jsonData; } /**** * 获取标签 * @param access_token * @return */ public static String getTags(String access_token){ String url ="https://api.weixin.qq.com/cgi-bin/tags/get?access_token=";//获取标签接口 String jsonData = TestMenu.sendGet(url+access_token,"utf-8",10000); System.out.println("请求接口返回"+jsonData); return jsonData; } /** * @param args */ public static void main(String[] args) { String appId = AppConst.APP_ID;//应用id String appSecret = AppConst.APPSECRET;//应用秘钥 //1,获取access_token String access_token = TestMenu.getToken(appId, appSecret); System.out.println("token值:"+access_token); //2,获取公众号已创建的标签 String tags = TestMenu.getTags(access_token); System.out.println("公众号已创建的标签:"+tags); } }
此文章本站原创,地址 https://www.vxzsk.com/165.html
转载请注明出处!谢谢!
感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程