天天看點

微信小程式擷取openid

小程式擷取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;
    }
}