mp_sig签名报错
发布于 6 年前 作者 caojuan 10190 次浏览 来自 问答

结果

2018-7-2 15:46:40.391 INFO [main] buy|jsonObject={
   "errcode": 90009,
   "errmsg": "mp_sig error hint: [eDd2eA05993139]"
}

加密方法

private static String getSign(StringBuilder stringBuilder, String session_key) {
        String mp_sig = null;
        try {
            String temp = SnsSigCheck.encodeUrl(stringBuilder.toString());
            Mac mac = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKey = new SecretKeySpec(session_key.getBytes(), "HmacSHA256");
            mac.init(secretKey);
            byte[] hash = mac.doFinal(stringBuilder.toString().getBytes());
            mp_sig = Base64.encodeBase64String(hash);
        } catch (Exception e) {
            Logs.get().error("=========e=" + Lang.getStackTrace(e));
        }
        return mp_sig;
    }

sig加密

StringBuilder stringBuilder = new StringBuilder("appid=");
        stringBuilder.append(appid);
        stringBuilder.append("&offer_id=");
        stringBuilder.append(offer_id);
        stringBuilder.append("&openid=");
        stringBuilder.append(openid);
        stringBuilder.append("&pf=android&ts=");
        stringBuilder.append(ts);
        stringBuilder.append("&zone_id=");
        stringBuilder.append(1);
        stringBuilder.append("&org_loc=/cgi-bin/midas/sandbox/getbalance&method=POST&secret="+secret);
        String sig = getSign(stringBuilder, secret);

mp_sig加密

stringBuilder = new StringBuilder("");
        stringBuilder.append("access_token=");
        stringBuilder.append(getAccessToken());
        stringBuilder.append("&appid=");
        stringBuilder.append(appid);
        stringBuilder.append("&offer_id=");
        stringBuilder.append(offer_id);
        stringBuilder.append("&openid=");
        stringBuilder.append(openid);
        stringBuilder.append("&pf=android");
        stringBuilder.append("&sig=");
        stringBuilder.append(sig);
        stringBuilder.append("&ts=");
        stringBuilder.append(ts);
        stringBuilder.append("&zone_id=");
        stringBuilder.append(1);
        stringBuilder.append("&org_loc=/cgi-bin/midas/sandbox/getbalance&method=POST&session_key="+session_key);

请求

LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("openid", openid);
        linkedHashMap.put("appid", appid);
        linkedHashMap.put("offer_id", offer_id);
        linkedHashMap.put("ts", ts);
        linkedHashMap.put("zone_id", zone_id);
        linkedHashMap.put("pf", pf);
        linkedHashMap.put("sig", sig);
        linkedHashMap.put("mp_sig", mp_sig);
        log.info("buy|linkedHashMap=" + Json.toJson(linkedHashMap, JsonFormat.nice()));
        String url = "https://api.weixin.qq.com/cgi-bin/midas/sandbox/getbalance?access_token=" + ACCESSTOKEN;
        Request req = Request.create(url, Request.METHOD.POST);
        req.getHeader().set("Content-Type", "application/octet-stream;charset=UTF-8");
        String jsonObject = Sender.create(req.setParams(linkedHashMap).setData(Json.toJson(linkedHashMap,JsonFormat.compact())))
                .setTimeout(10000).send().getContent("utf-8");
        log.info("buy|jsonObject=" + Json.toJson(Json.fromJson(NutMap.class, jsonObject), JsonFormat.nice()));
3 回复

看上去没什么问题,一些细节,比如ts是秒,签名算的值和用例子里的计算值是否相同,sessionKey是否正确之类的

你好。不需要重复发帖的,你的反馈我们已经收到,之前也有回复,有结果会在已回复的帖子下跟进哈

可是根本没人回复呀

回到顶部