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

/**
     * @param
     * @Description: 获取微信公众号用户登录信息
     * @date 2022/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 2022/04/22
 */
public class WxUtil {


    public static final String APP_ID = "*******";
    public static final String APP_SECRET = "*************";

    /**
     * 获取code后,请求以下链接获取access_token:
     *  https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE
     *  &grant_type=authorization_code
     */
    public static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=";


    public static final String ACCESS_TOKEN_URL_BY = "https://api.weixin.qq.com/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;
    }


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值