- 当前 Bug 的表现(可附上截图)
1、服务器端签名代码:
var token = ws.GetAccessToken(Appid, Secret); var timestamp = DateTime.Now.Ticks.ToString().Substring(0, 10); var jsapi_ticket = Main.GetTicket(token); string nonce_str = "1000" ; string carid = "p1SK9wg2SA3-0JFqzlddT6w53Gnc" ; string order = timestamp + jsapi_ticket + carid + nonce_str; string signature = Main.MakeSha1Sign(order); ret = ret.Replace( "{0}" , carid); ret = ret.Replace( "{1}" , nonce_str); ret = ret.Replace( "{2}" , timestamp); ret = ret.Replace( "{3}" , jsapi_ticket); ret = ret.Replace( "{4}" , signature); HttpContext.Current.Response.Write(ret); |
通过官方的签名校验工具,顺利通过
http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=cardsign
2、微信前端代码如下:
{ var timestamp = "" var signature = "" var cardid = "" var nonce_str wx.request({ url: app.globalData.host + '/action/Handler.ashx?a=ticket&sid=' + app.globalData.shopid, header: { 'content-type' : 'application/json' , // 默认值 "X-SQL-AppKey" : app.globalData.APPkey }, success(res) { if (res.data) { console.log(res.data) cardid = res.data.carid var cardExt={} cardExt.timestamp = res.data.timestamp cardExt.signature = res.data.signature cardExt.nonceStr = res.data.nonce_str wx.addCard({ cardList: [{ cardId: res.data.carid, cardExt: JSON.stringify(cardExt) }, ], success(res) { console.log(res.cardList) // 卡券添加结果 } }) } } }) } |
服务器端值正常接收,现在打开提示:签名错误!
服务器端返回代码:
{carid: "p1SK9wg2SA3-0JFqzlddT6w53Gnc" , nonce_str: "wxapp" , timestamp: "6369869659" , ticket: "IpK_1T69hDhZkLQTlwsAX2aXDM0lanFPJGw_1HYYb1bNF-NF70JbgTlOoJahIro2-ifNz4bHRR-8kdH2O4SuYQ" , signature: "04f21bd32a7a6d95a504fc066eeb928377a6e564" } |
cardExt: JSON.stringify(cardExt)
输出值是这个:
{ "timestamp" : "6369869659" , "signature" : "04f21bd32a7a6d95a504fc066eeb928377a6e564" , "nonceStr" : "wxapp" } |
小程序已经和公众号做了关联处理!
不知道哪里出问了问题,请求帮助,谢谢!
请问楼主是怎么解决的,我这边也是两个签名都一样但是小程序那边给提示签名错误。
问一下,那个卡券本身是不是有什么要求,我现在是用公众号appid和secret获取的token和ticket,校验也是一样的,但也是报签名错误
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type="wx_card"&offset_type=1";
获取ticket这个没错吧
代码排版太乱了,看着头疼。
建议上个代码片段
把后端返回的参数也发出来吧,还有打印下cardExt也发出来看看。
把时间戳改到目前时间,你这个起码是几十年后的了,然后是到秒的时间戳,还有nonce_str最好用随机字符串避免重复,还有你服务端的nonce_str是"1000",后面怎么又变成了wxapp。cardExt对象转成字符串的时候,试试用``模板语法拼接一下,别直接转成json字符串。
调整了,还是不行,服务器返回内容打印
{carid:
"p1SK9wg2SA3-0JFqzlddT6w53Gnc"
, nonce_str:
"636987"
, timestamp:
"2019071411520533"
, ticket:
"IpK_1T69hDhZkLQTlwsAX2aXDM0lanFPJGw_1HYYb1bNF-NF70JbgTlOoJahIro2-ifNz4bHRR-8kdH2O4SuYQ"
, signature:
"95c4f6ba8349a225e8e99510994a1ddb4a45c905"
}
与官方的验证工具对比一致
排序结果: 2019071411520533636987IpK_1T69hDhZkLQTlwsAX2aXDM0lanFPJGw_1HYYb1bNF-NF70JbgTlOoJahIro2-ifNz4bHRR-8kdH2O4SuYQp1SK9wg2SA3-0JFqzlddT6w53Gnc
签名结果: 95c4f6ba8349a225e8e99510994a1ddb4a45c905