Analysis of small micro-channel access procedure - Interface Signature verification request (JAVA Edition)

Foreword

The company has developed several small programs, small programs for easy viewing all data in real-time visitor, the official called directly query interface.
Call interface, the need to manually obtain a signature, the following is the summary of the process.

  1. Tencent Mobile Data URL: https://mta.qq.com/
  2. Small micro-channel program data api documentation: https://mta.qq.com/docs/wechat_mini_program_api.html

Small micro-channel data access program

Official signature

Here Insert Picture Description

Java version Tools

Note that this is the time stamp timestampin seconds.

  1. method
/** 
   * @Author luohongquan 
   * @Description 获取微信小程序分析接入接口签名:  https://mta.qq.com/docs/wechat_mini_program_api.html
   * @Date 15:12 2019/12/20
   * @Param [appId, secretKey, timestamp, startTime, endTime] 
   * @return java.lang.String 
   */ 
  public static String getWxAppDataSign(String appId, String secretKey, String timestamp, String startTime, String endTime) {
    long a = Calendar.getInstance().getTimeInMillis();
    String signStr = String.format(TencentMobileConstant.SIGN_TEMPLATE, secretKey, appId, endTime, startTime, timestamp);
    String sign = "";
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(signStr.getBytes());
      byte[] resultByteArray = md.digest();
      sign = new BigInteger(1, resultByteArray).toString(16);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sign;
  }
  1. test
  public static void main(String[] args) {
    String appId = "xxxx"; // 你的小程序appId
    String secretKey = "xxxxxxxxxxxxxxxxxxxxx"; // 你的小程序secretKey
    long a = Calendar.getInstance().getTimeInMillis();
    String timestamp = String.valueOf(a/1000);	// 秒
    String startTime = "2019-12-20";
    String endTime = "2019-12-20";
    String sign = getWxAppDataSign(appId, secretKey, timestamp, startTime,endTime);
  }

After acquiring signature, get access request https://openapi.mta.qq.com/wx/v1/analytics/period?start_time=2019-12-20&end_time=2019-12-20&timestamp=1576808602&app_id=xxxx&sign=3a26dd3d76bcb12d2a55fe253995exxx, to obtain the data.

Published 20 original articles · won praise 3 · Views 4531

Guess you like

Origin blog.csdn.net/qq_34246646/article/details/103635856