微信公众平台, 获取用户位置信息 config:invalid signature 一直报错误

u010001857 2016-10-25 04:48:09
前端页面代码 项目根目录下 MyJsp.jsp
<%
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>


java 方法 HttpXmlClient.createShareSign(noncestr, timestamp, url) 代码
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;
}


我访问http://xxxxx/zwdtSW/MyJsp.jsp 页面 是一直提示{“errMsg”:"getLocation:invalid signature"}
希望各位网友帮忙解决下
...全文
1321 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tt6550619 2016-12-25
  • 打赏
  • 举报
回复
楼主解决没有 我也是同样的问题
u010001857 2016-10-26
  • 打赏
  • 举报
回复
我所用到的参数 我都打印出来了 对比了 但是没发现问题在什么地方
u010001857 2016-10-26
  • 打赏
  • 举报
回复
引用 7 楼 yjc_1111 的回复:
[quote=引用 6 楼 u010001857 的回复:]
这三个地方 已经确定一致

你看看你后台返回的json是什么,那有没有验证通过。
另外确保你实际的url与你验证时的url一致。[/quote]
String str 的值jsapi_ticket=kgt8ON7yVITDhtdwci0qeZj25EmIo1f8bDZDGya6zER4qtrHgOY0DaG72IMFft1fJ72D8YOcG79HCwB_-RSiUw&noncestr=cbf8b0a9-48c6-41c3-a2ee-40c518255242×tamp=1477451878&url=http://xxx/zwdtSW/MyJsp.jsp?params=1477451878
Small-Young 2016-10-26
  • 打赏
  • 举报
回复
引用 6 楼 u010001857 的回复:
这三个地方 已经确定一致
你看看你后台返回的json是什么,那有没有验证通过。 另外确保你实际的url与你验证时的url一致。
u010001857 2016-10-26
  • 打赏
  • 举报
回复
这三个地方 已经确定一致
Small-Young 2016-10-26
  • 打赏
  • 举报
回复
引用 4 楼 u010001857 的回复:
引用
yjc_1111
你两个timestamp不一样,你js用的timestamp与你验证的timestamp不是一个。

我是用变量保存 timestamp,然后使用这个变量去验证和js使用.是同一个值的


这三个值要保持一致。
u010001857 2016-10-26
  • 打赏
  • 举报
回复
引用
yjc_1111 你两个timestamp不一样,你js用的timestamp与你验证的timestamp不是一个。
我是用变量保存 timestamp,然后使用这个变量去验证和js使用.是同一个值的
Small-Young 2016-10-26
  • 打赏
  • 举报
回复
你两个timestamp不一样,你js用的timestamp与你验证的timestamp不是一个。
u010001857 2016-10-26
  • 打赏
  • 举报
回复
我获取access_token 的代码
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;
	}
家里敷泥呀 2016-10-26
  • 打赏
  • 举报
回复
token校验失败,参考: 微信公众平台接入Java实例,结合BAE - JAVA编程语言程序开发技术文章 - 红黑联盟 http://www.2cto.com/kf/201405/299487.html (3.4.实现验证TOKEN方法)
Small-Young 2016-10-26
  • 打赏
  • 举报
回复

敢不敢把这些单引号去掉试试。。。。。
悲鸣秋 2016-10-26
  • 打赏
  • 举报
回复
invalid signature签名错误。建议按如下顺序检查: 1 确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验。 2确认config中nonceStr(js中驼峰标准大写S), timestamp与用以签名中的对应noncestr, timestamp一致。 3确认url是页面完整的url(请在当前页面alert(location.href.split('#')[0])确认),包括'http(s)://'部分,以及'?'后面的GET参数部分,但不包括'#'hash后面的部分。 4 确认 config 中的 appid 与用来获取 jsapi_ticket 的 appid 一致。 5确保一定缓存access_token和jsapi_ticket。 6确保你获取用来签名的url是动态获取的,动态页面可参见实例代码中php的实现方式。如果是html的静态页面在前端通过ajax将url传到后台签名,前端需要用js获取当前页面除去'#'hash部分的链接(可用location.href.split('#')[0]获取,而且需要encodeURIComponent),因为页面一旦分享,微信客户端会在你的链接末尾加入其它参数,如果不是动态获取当前链接,将导致分享后的页面签名失败。 把 String url = request.getRequestURL().toString();//当前访问链接 if(request.getQueryString()!= null && !"".equals(request.getQueryString())){ url += "?"+ request.getQueryString(); } 试试
u010001857 2016-10-26
  • 打赏
  • 举报
回复
引用 10 楼 yjc_1111 的回复:
[quote=引用 9 楼 u010001857 的回复:]
我所用到的参数 我都打印出来了 对比了 但是没发现问题在什么地方

你微信后台有没有绑定一级域名,测试有没有在这个绑定的一级域名下。[/quote]

有的
Small-Young 2016-10-26
  • 打赏
  • 举报
回复
引用 9 楼 u010001857 的回复:
我所用到的参数 我都打印出来了 对比了 但是没发现问题在什么地方
你微信后台有没有绑定一级域名,测试有没有在这个绑定的一级域名下。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧