失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > JAVA对接微信公众号获取-用户信息

JAVA对接微信公众号获取-用户信息

时间:2024-04-17 23:03:23

相关推荐

JAVA对接微信公众号获取-用户信息

/*** @param* @Description: 获取微信公众号用户登录信息* @date /04/22*/public JSONObject selectWxUser(String code) {JSONObject jsonWxUser = new JSONObject();Map map = new HashMap<>();CloseableHttpClient httpclient = HttpClients.createDefault();//调用微信接口HttpGet httpget = new HttpGet(WxUtil.GET_ACCESS_TOKEN_URL + WxUtil.APP_ID+ "&secret=" + WxUtil.APP_SECRET + "&code=" + code + "&grant_type=authorization_code");CloseableHttpResponse response = null;try {response = httpclient.execute(httpget);HttpEntity entity = response.getEntity();JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(entity));//获取微信公众号用户openid返回的数据String openid = jsonObject.getString("openid");log.info("openid数据为:"+openid);//根据openid 去数据库查询是否存在String accesstToken = jsonObject.getString("access_token");String scope = jsonObject.getString("scope");log.info("scope:"+scope);//通过openid以及accesstToken 获取用户信息// 拉取用户信息(需scope为 snsapi_userinfo)String infoUrl = WxUtil.ACCESS_TOKEN_URL_BY+accesstToken+ "&openid="+openid + "&lang=zh_CN";log.info("infoUrl:"+infoUrl);JSONObject userInfo = WeChatUtils.doGetJson(infoUrl);String nickName = userInfo.getString("nickname");//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知String headimgurl = userInfo.getString("headimgurl");log.info("用户昵称:{}", nickName);log.info("用户头像:{}", headimgurl);map.put("phone", null);map.put("openid", openid);map.put("nickName", nickName);map.put("headimgurl", headimgurl);jsonWxUser.put("code", 0);jsonWxUser.put("data", map);} catch (IOException e) {log.info("后台报错:{}", e.toString());}return jsonWxUser;}

package com.rmsz.oa.utils;/*** @Author 微信工具类* @Date /04/22*/public class WxUtil {public static final String APP_ID = "*******";public static final String APP_SECRET = "*************";/*** 获取code后,请求以下链接获取access_token:* https://api./sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE* &grant_type=authorization_code*/public static final String GET_ACCESS_TOKEN_URL = "https://api./sns/oauth2/access_token?appid=";public static final String ACCESS_TOKEN_URL_BY = "https://api./sns/userinfo?access_token=";}

package com.rmsz.oa.utils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class WeChatUtils {/*** @param url* @return* @throws Exception*/public static JSONObject doGetJson(String url){JSONObject jsonObject =null;DefaultHttpClient client = new DefaultHttpClient();HttpGet httpGet =new HttpGet(url);HttpResponse response = null;try {response = client.execute(httpGet);HttpEntity entity =response.getEntity();if(entity!=null){//把返回的结果转换为JSON对象String result = EntityUtils.toString(entity, "UTF-8");jsonObject = JSON.parseObject(result);}} catch (IOException e) {e.printStackTrace();}return jsonObject;}/*** shal加密* @param src* @return*/public static String shal(String src){//获取一个加密对象MessageDigest md = null;try {md = MessageDigest.getInstance("sha1");char[] chars={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};StringBuilder sb = new StringBuilder();//加密byte[] digest = md.digest(src.getBytes());//处理加密结果for (byte b : digest) {sb.append(chars[(b>>4)&15]) ;sb.append(chars[b&15]);}return sb.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}}

如果觉得《JAVA对接微信公众号获取-用户信息》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。