在客户端调用midasGetBalance接口在沙箱环境查询余额,按照文档要求进行数据签名,然后进行请求,但是一直返回
{"errcode":90009,"errmsg":"mp_sig error hint: [mdJ5.a06883102]"}
具体代码如下,其中crypto是node.js中的crypto.js模块:
let ts = Math.round( new Date().getTime()/1000); //通过wx.login的code换取的openid和session_key //通过https://api.weixin.qq.com/cgi-bin/token获取的access_token //appid、offer_id和midasSecret已经申请好的 let data = { openid: LoginUser.wx_openid, appid: appid, midasSecret: midasSecret, offer_id: offer_id, ts: ts, zone_id: '1' , pf: 'android' , access_token: LoginUser.wx_access_token, session_key: LoginUser.wx_session_key }; let stringA = 'appid=' +data.appid; stringA += '&offer_id=' + data.offer_id; stringA += '&openid=' + data.openid; stringA += '&pf=' +data.pf; stringA += '&ts=' +data.ts; stringA += '&zone_id=' +data.zone_id; let sigTemp = stringA+ "&org_loc=/cgi-bin/midas/getbalance&method=POST&secret=" +data.midasSecret; //sig参数 let sig = crypto.createHmac( 'sha256' ,data.midasSecret).update(sigTemp).digest( 'hex' ); data.sig = sig; let stringB = 'access_token=' + data.access_token; stringB += '&appid=' + data.appid; stringB += '&offer_id=' + data.offer_id; stringB += '&openid=' + data.openid; stringB += '&pf=' + data.pf; stringB += '&sig=' + sig; stringB += '&ts=' + data.ts; stringB += '&zone_id=' + data.zone_id; let mp_sigTemp = stringB+ "&org_loc=/cgi-bin/midas/getbalance&method=POST&session_key=" +data.session_key; //mp_sig let mp_sig = crypto.createHmac( 'sha256' ,data.session_key).update(mp_sigTemp).digest( 'hex' ); data.mp_sig = mp_sig; let resultData = data; let xhr = cc.loader.getXMLHttpRequest(); //沙箱环境 let url = 'https://api.weixin.qq.com/cgi-bin/midas/sandbox/getbalance?' ; url += 'access_token=' +resultData.access_token; xhr.open( 'POST' ,url, true ); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
if (response.errcode === 0){ //查询成功 console.log( '查询成功' ); } else { //查询失败 console.log( '查询失败' ); } } }; //POST参数 let form = { openid: resultData.openid, appid: resultData.appid, offer_id: resultData.offer_id, ts: resultData.ts, zone_id: resultData.zone_id, pf: resultData.pf, sig: resultData.sig, access_token: resultData.access_token, mp_sig: resultData.mp_sig }; xhr.send(JSON.stringify(form)); |
POST的数据如下:
{"openid":"o6iRc5Tm5tZH7k6zRgWqBMuMpzH8","appid":"wx67f6b13c452ed6bb","offer_id":"1450014357","ts":1522721483,"zone_id":"1","pf":"android","sig":"4b9a04df8600bcbe2c24313d594820a0b865e70cff966acc28c59c6fc5825e3c","access_token":"8_8UqjUd49YcgukSHPRSfdLXrj2YsRzxUSaKgPtYZwkKGR4tVr6oAGvtOXGDTJDuI7Jkx8l8zgS9UDHpVfNaojOqvGk-2F-GdzbF3cTuVrRY6EwuQr_49Yu0bs3JEH33_IRMNVOEOqg8RNMrfkARLiAIAGXR","mp_sig":"252c78694e9ca1491e812476c4d97efcbbc89c215ab1e37f407580a70c6646b5"}
我用的php算签名:
$sign = hash_hmac("sha256", $source, $secret);
return $sign;
也是一直报 90009 错误{"errcode":90009,"errmsg":"mp_sig error hint: [K6oh2a02954546]"}
@yefeng
session_key 过期也可能会造成 mp_sig error 的
@田顺建 @Buger
请问你们
wx.requestMidasPayment
都成功了,为什么我总是返回1003错误,看了是米大师的portal错误,但是没有具体解释,很苦恼
恩 对 最好都是后端去调用
@Buger
谢谢指正,改成沙盒URI查询成功了。
另外,我看你们都是在后端调这个midasGetBalance接口的。小游戏与游戏币有关的那几个接口,是不是除了
wx.requestMidasPayment之外,都是建议后端去调用呢?
我在沙盒也是用 /cgi-bin/midas/sandbox/getbalance,都没用,还是签名错
@田顺建 org_loc 的值沙箱环境 用 /cgi-bin/midas/sandbox/getbalance
我这里也是相同情况,不知道怎么解决,我按文档上的参数,生成的签名是跟文档一样的,但是当我使用该签名方法生成签名向微信请求时就说{"errcode":90009,"errmsg":"mp_sig error hint: [mdJ5.a06883102]"}
和你一模一样 不知道你解决了吗 大佬