Java微信公众号开发之通过微信公众号获取用户信息
简介
本文将详细讲解如何通过Java实现微信公众号获取用户信息细节,包括获取用户基本信息和获取关注者列表,最后提供两条常用的示例说明。
准备工作
在开始获取用户信息之前,你需要进行以下步骤的准备工作:
- 注册微信公众号,并获取关注者OpenID和Access Token.
- 创建Java Web服务器,并引入相关依赖包,推荐使用Spring Boot或Spring Cloud等快速开发工具。
- 借助微信官方提供的API,通过Java实现获取用户信息的相关功能。
获取用户基本信息
获取用户基本信息,需要先获取用户的OpenID,再通过微信API获取相应用户信息。
获取用户OpenID
获取用户OpenID并不是用户主动授权的过程,而是根据用户关注公众号的情况,自动为用户分配唯一的OpenID。获取用户OpenID的API为:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
其中,ACCESS_TOKEN为Access Token,OPENID为用户的OpenID。
通过OpenID获取用户基本信息
通过获取到的用户OpenID,再通过微信API获取用户基本信息。获取用户基本信息的API为:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
其中,ACCESS_TOKEN为Access Token,OPENID为用户的OpenID。
Java代码示例:
// 获取用户OpenID
String openid = "xxxxxx";
// 获取Access Token
String accessToken = "xxxxxxxxxxxxxxx";
// 获取用户基本信息
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
String userInfo = buffer.toString();
获取关注者列表
获取关注者列表,需要借助微信API提供的以下URL:
https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
其中,ACCESS_TOKEN为Access Token,NEXT_OPENID为分页查询的下一个用户的OpenID,第一次查询可使用“”代替。
Java代码示例:
// 获取Access Token
String accessToken = "xxxxxxxxxxxxxxx";
// 获取用户列表
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=";
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
String userList = buffer.toString();
示例说明
以下提供两个示例,供参考:
示例1:获取所有关注者OpenID
// 获取Access Token
String accessToken = "xxxxxxxxxxxxxxx";
String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=";
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
String userList = buffer.toString();
// 解析返回结果,获取所有关注者OpenID
JSONObject userJson = JSONObject.fromObject(userList);
String nextOpenId = userJson.getString("next_openid");
JSONArray jsonArray = userJson.getJSONObject("data").getJSONArray("openid");
List<String> openIds = new ArrayList<String>();
for (int i = 0; i < jsonArray.size(); i++) {
openIds.add(jsonArray.getString(i));
}
// 循环获取所有关注者基本信息
for (String openid : openIds) {
String url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
URL requestUrl2 = new URL(url2);
HttpURLConnection conn2 = (HttpURLConnection) requestUrl2.openConnection();
conn2.setRequestMethod("GET");
conn2.setDoOutput(true);
conn2.setDoInput(true);
conn2.connect();
// 将返回的输入流转换成字符串
InputStream inputStream2 = conn2.getInputStream();
StringBuffer buffer2 = new StringBuffer();
byte[] bytes2 = new byte[1024];
int len2 = 0;
while ((len2 = inputStream2.read(bytes2)) != -1) {
buffer2.append(new String(bytes2, 0, len2));
}
String userInfo = buffer2.toString();
// 解析返回结果,获取关注者基本信息
JSONObject userJson2 = JSONObject.fromObject(userInfo);
String nickname = userJson2.getString("nickname");
String sex = userJson2.getString("sex");
String province = userJson2.getString("province");
}
示例2:获取某个关注者的详细信息
// 获取Access Token
String accessToken = "xxxxxxxxxxxxxxx";
String openid = "xxxxxx";
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openid + "&lang=zh_CN";
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
buffer.append(new String(bytes, 0, len));
}
String userInfo = buffer.toString();
// 解析返回结果,获取关注者详细信息
JSONObject userJson = JSONObject.fromObject(userInfo);
String nickname = userJson.getString("nickname");
String sex = userJson.getString("sex");
String city = userJson.getString("city");
String country = userJson.getString("country");
String province = userJson.getString("province");
String language = userJson.getString("language");
String headImgUrl = userJson.getString("headimgurl");
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java微信公众号开发之通过微信公众号获取用户信息 - Python技术站