微信小程序获取openid

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43473714/article/details/86535568

小程序获取openid

private static final String APPID = "xxx";
    private static final String SECRET = "xxx";
    private static final String GRANT_TYPE = "authorization_code";

    @GetMapping("getOpenId")
    public WX getOpenId(String code){//code是小程序前端传到后台的
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        Map param = new HashMap<>();
        param.put("appid",APPID);
        param.put("secret",SECRET);
        param.put("js_code",code);
        param.put("grant_type",GRANT_TYPE);
        JSONObject object=JSONObject.fromObject(HttpClientUtil.doGet(url,param));
        WX wx = (WX) JSONObject.toBean(object,WX.class);
        return wx;
    }

获取openid时得到的实体类

public class WX {

    /*用户唯一标识*/
    private String openid;
    /*会话密钥*/
    private String session_key;
    /*用户在开放平台的唯一标识符,在满足 UnionID 下发条件的情况下会返回,详见 UnionID 机制说明。*/
    private String unionid;
    /*错误码*/
    private String errcode;
    /*错误信息*/
    private String errmsg;
}

HttpClientUtil工具类

public class HttpClientUtil {
    public static String doGet(String url, Map<String, String> param) {

        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            HttpGet httpGet = new HttpGet(uri);

            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }

    public static String doGet(String url) {
        return doGet(url, null);
    }

    public static String doPost(String url, Map<String, String> param) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return resultString;
    }

    public static String doPost(String url) {
        return doPost(url, null);
    }

    public static String doPostJson(String url, String json) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            HttpPost httpPost = new HttpPost(url);
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultString;
    }
}
展开阅读全文

没有更多推荐了,返回首页