# OpenApi SDK
- java版本:java-open-sdk 2.0.1.zip (opens new window)
- python版本:python-open-sdk 2.0.2.zip (opens new window)
- php版本:php-open-sdk 2.0.1.zip (opens new window)
- node版本:node-open-sdk 2.0.1.zip (opens new window)
- .net版本:dotnet-open-sdk 2.0.1.zip (opens new window)
- go版本:go-open-sdk 2.0.1.zip (opens new window)
请求结构如下:
http://open.${区域}.fenydata.com/fenydata-java-open/?Action=****&Parameters
java代码示例
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fenydata.openSdk.util.SignatureUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Application {
public static void main(String[] args) throws Exception {
String method = "POST";
String AccessKeyId = "X14Tbb*************";
String AccessKeySecret = "OmQQ***************************";
Map<String, String> map = new HashMap<>();
map.put("AccessKeyId", AccessKeyId);
map.put("Version", "2018-01-20");
map.put("SignatureMethod", "HMAC-SHA1");
map.put("Timestamp", new Date().toString());
map.put("SignatureVersion", "1.0");
map.put("SignatureNonce", RandomUtil.randomString(10));
map.put("RegionId", "huadong2");
map.put("Format", "JSON");
// 透传下发二进制数据
String hexStr = "1a7f3c8e0d9b2a5f4e6c9d8a7b0c3d2e1f5a6b4c8d9e0f7a2b3c4d5e6f8091a2b3c1a7f3c8e0d9b2a5f4e6c9d8a7b0c3d2e1f5a6b4c8d9e0f7a2b3c4d5e6f8091a2b3c";
byte[] bytes = HexUtil.decodeHex(hexStr);
String hexMessageContent = Base64.encode(bytes);
// 下发文本或JSON字符串
String jsonStr = "{\n" +
" \"msgId\": \"165399880288699493\",\n" +
" \"msgType\": \"RealTimeData\",\n" +
" \"data\": {\n" +
" \"hex\": \"5103FE0C8C0CB30CA20C930C8C0C8C0CB50C9900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FF00FF00FF00FF00FF00FF01027530025E0007000800020CB500070C8C0001002900000000000000010000000000000000012F000000007530000000000000000000000000000000000C9900000000000A00FF0000000000000000753019070B0A31181716000000000000000000007530000002890000808000000000000080000000753000000000000100007DF1FFFFFFFFFFFFFFFF0002F6AE\"\n" +
" },\n" +
" \"timestamp\": 1752202164\n" +
"}";
String jsonMessageContent = Base64.encode(jsonStr);
map.put("Action", "Pub");
map.put("IotInstanceId", "709123984095973376");
map.put("ProductKey", "iI6sfVSP5yR");
map.put("TopicFullName", "/iI6sfVSP5yR/RkATTMZeNRkcLO9YEeXC/user/get");
map.put("MessageContent", jsonMessageContent);
map.put("Qos", "0");
// sign
String signature = SignatureUtils.generate(method, map, AccessKeySecret);
// url
String baseUrl = "http://open.huadong2.fenydata.com/fenydata-java-open/";
if ("GET".equals(method)) {
// url + Signature
baseUrl += "?Signature=" + signature + "&";
// url + 其他参数
List<String> keys = map.keySet().stream().toList();
StringBuilder requestUrl = new StringBuilder(baseUrl);
for (String key : keys) {
String val = map.get(key);
String k = URLUtil.encode(key);
String v = URLUtil.encode(val);
requestUrl.append(k).append("=").append(v).append("&");
}
requestUrl = new StringBuilder(requestUrl.substring(0, requestUrl.length() - 1));
System.out.println(StrUtil.format("GET url:{}", requestUrl));
String res = HttpUtil.get(requestUrl.toString());
System.out.println("res:" + res);
} else {
map.put("Signature", signature);
System.out.println(StrUtil.format("POST url:{},body:{}", baseUrl, JSONUtil.toJsonStr(map)));
String res = HttpUtil.post(baseUrl, JSONUtil.toJsonStr(map));
System.out.println("res:" + res);
}
}
}