微信小程序> 微信小程序获取openid

微信小程序获取openid

浏览量:2675 时间: 来源:hester呀

小程序获取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;    }}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎