微信公众号转发网页

public ResultData getCcessToken(String param) {
RequestParam req = null;
Map parmap = null;
try {
req = gm.jsonToAnyObject(param, RequestParam.class);
parmap = req.getValueOfClz(Map.class);
} catch (Exception e) {
return UtilParm.resultData(0, "param is err", null);
}
//获取access_token
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxxxxxxxxxxxxxxxxx&secret=xxxxxxxxxxxxxxxx";
String access_token = null;
String jsapi_ticket = null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity());
JSONObject jsonResult = new JSONObject(strResult);
access_token = (String) jsonResult.get("access_token");
}
} catch (Exception e) {
throw new RuntimeException("获取access_token出错"+e.getMessage());
}
//获取jsapi_ticket
String url2 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";
URI u = null;
try {
u = new URI(url2);
} catch (URISyntaxException e) {
throw new RuntimeException("jsapi_ticket地址出错"+e.getMessage());
}
request.setURI(u);
try {
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(response.getEntity());
JSONObject jsonResult = new JSONObject(strResult);
jsapi_ticket = (String) jsonResult.get("ticket");
}
} catch (Exception e) {
throw new RuntimeException("获取jsapi_ticket出错"+e.getMessage());
}
SortedMap map = new TreeMap<>();
String timeStamp = TimeStampUtil.getTimeStamp();
String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0,16);
map.put("jsapi_ticket", jsapi_ticket);
map.put("noncestr", noncestr);
map.put("timestamp", timeStamp);
String str = parmap.get("url").toString();
//反编译验证地址
map.put("url", WxURLDecoderUtil.getResDecoderUtil(str));
String wxSplit = WxSpliceUtil.getWxSplit(map);
String shaEncode;
try {
//sha1加密
shaEncode = SecuritySHA1Utils.shaEncode(wxSplit);
} catch (Exception e) {
throw new RuntimeException("获取shi加密失败");
}
map.put("appId", "xxxxxxxxxxxx");
map.put("signature", shaEncode);
return UtilParm.resultData(1, "ok", map);
}

你可能感兴趣的:(微信公众号转发网页)