先吐槽一波 微信真尼玛坑
本篇文章献给跟我一样第一次接触微信的小白,纯属个人见解,不喜勿喷
先奉上关于获取微信用户信息的代码
public static com.alibaba.fastjson.JSONObject getUserInfo() {
com.alibaba.fastjson.JSONObject jsonObject = null;
//https://api.weixin.qq.com/sns/userinfo微信获取用户信息的接口
String uri ="https://api.weixin.qq.com/sns/userinfo?access_token=" +
ACCESS_TOKEN+
"&openid=" +
OPENID;
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(URI.create(uri));
try {
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String temp = reader.readLine(); temp != null; temp = reader.readLine()) {
builder.append(temp);
}
jsonObject = com.alibaba.fastjson.JSONObject.parseObject(builder.toString().trim());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
有的小伙伴可能看到返回的json中根本就没有unionid这个key,去看微信官方文档你会发现这个一段话
注意你要去微信开放平台注册账号然后绑定你的工众号就可以了
我在这个坑里爬了一天才爬出来 Google了半天都没有具体一个的解答 当然也可能是我理解能力太差了
虽然微信文档很坑但还是要多看看啊