81,092
社区成员
发帖
与我相关
我的任务
分享
<%
String webRoot = AppContext.webRootPath;
String appId = ITMConstant.WX_APP_ID;
String noncestr = UUID.randomUUID().toString();
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
String url = "http://xxxxx/zwdtSW/MyJsp.jsp"; //为了排除url不一致可能导致签名不一致 我写死url
String signature = HttpXmlClient.createShareSign(noncestr, timestamp, url);
%>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
//定位
$(function(){
wx.config({
debug: true, // 调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '<%=appId%>',
timestamp: <%=Long.parseLong(timestamp)%>, // 生成签名的时间戳
nonceStr: '<%=noncestr%>',
signature: '<%=signature%>',
jsApiList: [
'openLocation',
'getLocation'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
});
wx.error(function(res){
});
});
function submitOrderInfoClick(){
wx.getLocation({
success: function (res) {
alert("获取地理位置成功,经纬度为:(" + res.latitude + "," + res.longitude + ")" );
},
fail: function(error) {
alert("获取地理位置失败");
}
});
}
</script>
</head>
public static String createShareSign(String noncestr,String timestamp,String url) throws NoSuchAlgorithmException, UnsupportedEncodingException{
Map<String, String> params = new HashMap<String, String>();
//获取access_token
params.put("access_token",ChatUtil.getAccess_token());
params.put("type", "jsapi");
String xml = HttpXmlClient.post("https://api.weixin.qq.com/cgi-bin/ticket/getticket",params);
JSONObject jsonMap = JSONObject.fromObject(xml);
Map<String, String> map = new HashMap<String, String>();
Iterator<String> it = jsonMap.keys();
while(it.hasNext()) {
String key = (String) it.next();
String u = jsonMap.get(key).toString();
map.put(key, u);
}
//获取ticket
String jsapi_ticket = map.get("ticket");
String str = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + noncestr +
"×tamp=" + timestamp +
"&url=" + url;
//sha1加密
String signature = "";
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(str.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
return signature;
}
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
yjc_1111 你两个timestamp不一样,你js用的timestamp与你验证的timestamp不是一个。
public static String getAccess_token() {
String accessToken = null;
try {
String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type="
+ ITMConstant.GRANT_TYPE
+ "&appid="
+ ITMConstant.WX_APP_ID
+ "&secret=" + ITMConstant.WX_APP_SECRET;
URL urlGet = new URL(null, tokenUrl, new sun.net.www.protocol.https.Handler());
HttpURLConnection http = (HttpURLConnection) urlGet
.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, ITMConstant.ENCODE_UTF_8);
JSONObject demoJson = JSONObject.fromObject(message);
accessToken = demoJson.getString("access_token");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
[quote=引用 9 楼 u010001857 的回复:]
我所用到的参数 我都打印出来了 对比了 但是没发现问题在什么地方
我所用到的参数 我都打印出来了 对比了 但是没发现问题在什么地方