收藏
回答

mp_sig签名报错

框架类型 问题类型 操作时间 AppID
小游戏 Bug 2018-07-02 wxe0e6a8746dd227ed

结果

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 个回答

  • 2018-07-03

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

    2018-07-03
    有用
    回复 2
    • 2018-07-03

      sessionKey你是用的哪个接口获得的值?

      String httpCode = "https://api.weixin.qq.com/sns/jscode2session";
              Response response = Sender.create(Request.create(httpCode, Request.METHOD.GET).setParams(map)).setTimeout(60000).send();
              String content = response.getContent("utf-8");
              NutMap nutMap = Json.fromJson(NutMap.class, content);
              String openId = nutMap.getString("openid");
              String session_key = nutMap.getString("session_key");

      我用的sessionKey是这个接口里面获得的值

      2018-07-03
      回复
    • 2018-07-03回复

      我这块登录接的H5SDK的,先和H5SDK交互,获取openId和sessionId,在用这个信息去获取的SessionKey

      2018-07-03
      回复
  • 2018-07-02

    可是根本没人回复呀

    2018-07-02
    有用
    回复
  • 疯狂的小辣椒
    疯狂的小辣椒
    2018-07-02

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

    2018-07-02
    有用
    回复
登录 后发表内容