Skip to content

鉴权

参数

Header中须包含字段:
sign(签名)
ak(标识)
timestamp(时间戳)

签名方式

Authorization签名需要的ak,sk需联系商务获取。

签名方法

java
    import cn.hutool.core.map.MapUtil;
    import cn.hutool.json.JSONUtil;
    import com.qiniu.util.StringUtils;
    import okhttp3.Call;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import org.apache.commons.codec.digest.DigestUtils;
    import org.springframework.util.ObjectUtils;

    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    import java.util.HashMap;
    import java.util.Map;

    public class JavaDemo {
        public static String ak = "你的ak,联系商务获取";
        public static String sk = "你的sk,联系商务获取";

        public static String baseUrl = "https://openapi.biandianyun.com";


        public static void main(String[] args) throws Exception {
            get();

            post();
        }

        public static void get() throws Exception {
            Map<String, Object> data = new HashMap<>();
            data.put("pageNo", 1);
            data.put("pageSize", 100);

            String requestUrl = "/v1/device/queryDeviceByPage";
            if (data != null) {
                String queryStr = MapUtil.join(data, "&", "=");
                requestUrl += "?" + queryStr;
            }
            requestUrl = baseUrl + requestUrl;

            long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp, null);

            OkHttpClient client = new OkHttpClient.Builder().build();

            Request request = new Request.Builder()                .header("ak", ak)
                    .header("timestamp", timestamp + "")
                    .header("sign", sign)
                    .url(requestUrl)
                    .get()
                    .build();

            Call call = client.newCall(request);
            String resp = call.execute().body().string();

            System.out.println(resp);
        }

        public static void post() throws Exception {
            String requestUrl = "/v1/device/submit";
            // 组装接口逻辑处理的请求参数
            Map<String, Object> data = new HashMap<>();
            data.put("sn", "xxxxxxxxxxxx");

            long timestamp = System.currentTimeMillis();
            String sign = getSign(timestamp, data);
            RequestBody reqBody = RequestBody.create(StringUtils.utf8Bytes(JSONUtil.toJsonStr(data)));
            // 接口的完整请求url : 域名+path
            requestUrl = baseUrl + requestUrl;
            Request request = new Request.Builder()
                    .header("Content-Type", "application/json")
                    .header("ak", ak)
                    .header("timestamp", timestamp + "")
                    .header("sign", sign)
                    .url(requestUrl)
                    .post(reqBody)
                    .build();

            OkHttpClient client = new OkHttpClient.Builder().build();
            Call call = client.newCall(request);
            String resp = call.execute().body().string();

            System.out.println(resp);
        }

        public static String getSign(long timestamp, Map<String, Object> data) {
            String jsonStr = ObjectUtils.isEmpty(data) ? null : JSONUtil.toJsonStr(data);
            return DigestUtils.sha256Hex(Base64.getEncoder().encodeToString((ak + sk + jsonStr + timestamp).getBytes(StandardCharsets.UTF_8)));
        }
    }