赞
踩
创建产品,添加操作设备,方式:HTTP,文件类型:图片和文本
平台文件管理文档:https://open.iot.10086.cn/doc/v5/fuse/detail/991
https://open.iot.10086.cn/doc/v5/fuse/detail/1402
在设备管理下找到操作设备,上传的文件
先进行API鉴权,经过平台认证才能通过Postman发出Post请求
postman官网:Download Postman | Get Started for Free
官方文档:https://open.iot.10086.cn/doc/v5/fuse/detail/1402
请求方式:post
请求头:
Content-Type | multipart/form-data |
Authorization | API鉴权 |
最新版onenet平台提供两种鉴权方式:设备接入安全鉴权、API调用鉴权
https://blog.csdn.net/ab23456_/article/details/135711971?spm=1001.2014.3001.5501
看的这篇文章
文档:https://open.iot.10086.cn/doc/v5/fuse/detail/1464
在线编辑器:https://www.jyshare.com/compile/9/
新版没有相关的代码生成,旧版的开发文档有多种语言代码(其中Python代码的et过期了要修改才能用)
https://open.iot.10086.cn/doc/v5/develop/detail/635
生成Authorization的Java代码:
version:2022-05-01(区别于设备接入)
res:userid/用户id(用户级于平台对接)
access_key:用户的密钥(在用户中心里要手机验证码)
- import javax.crypto.Mac;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.security.InvalidKeyException;
- import java.security.NoSuchAlgorithmException;
- import java.util.Base64;
- public class Token {
-
- public static String assembleToken(String version, String resourceName, String expirationTime, String signatureMethod, String accessKey)
- throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
- StringBuilder sb = new StringBuilder();
- String res = URLEncoder.encode(resourceName, "UTF-8");
- String sig = URLEncoder.encode(generatorSignature(version, resourceName, expirationTime, accessKey, signatureMethod), "UTF-8");
- sb.append("version=")
- .append(version)
- .append("&res=")
- .append(res)
- .append("&et=")
- .append(expirationTime)
- .append("&method=")
- .append(signatureMethod)
- .append("&sign=")
- .append(sig);
- return sb.toString();
- }
-
- public static String generatorSignature(String version, String resourceName, String expirationTime, String accessKey, String signatureMethod)
- throws NoSuchAlgorithmException, InvalidKeyException {
- String encryptText = expirationTime + "\n" + signatureMethod + "\n" + resourceName + "\n" + version;
- String signature;
- byte[] bytes = HmacEncrypt(encryptText, accessKey, signatureMethod);
- signature = Base64.getEncoder().encodeToString(bytes);
- return signature;
- }
-
- public static byte[] HmacEncrypt(String data, String key, String signatureMethod)
- throws NoSuchAlgorithmException, InvalidKeyException {
- //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
- SecretKeySpec signinKey = null;
- signinKey = new SecretKeySpec(Base64.getDecoder().decode(key),
- "Hmac" + signatureMethod.toUpperCase());
-
- //生成一个指定 Mac 算法 的 Mac 对象
- Mac mac = null;
- mac = Mac.getInstance("Hmac" + signatureMethod.toUpperCase());
-
- //用给定密钥初始化 Mac 对象
- mac.init(signinKey);
-
- //完成 Mac 操作
- return mac.doFinal(data.getBytes());
- }
-
- public enum SignatureMethod {
- SHA1, MD5, SHA256;
- }
-
- public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
- String version = "2022-05-01";
- String resourceName = "userid/369088";
- String expirationTime = System.currentTimeMillis() / 1000 + 100 * 24 * 60 * 60 + "";
- String signatureMethod = SignatureMethod.SHA1.name().toLowerCase();
- String accessKey = "Li44KN9s9TrCirdGA+mkNv+mj4x1tld0oxjD2d3es6NrkjKj3PTQz18AREreEEMUAfYEEvQFsw4nWObiZItYfw==";
- String token = assembleToken(version, resourceName, expirationTime, signatureMethod, accessKey);
- System.out.println("Authorization:" + token);
- }
- }
得到Authorization类似如下
version=2022-05-01&res=userid%2F用户id&et=1714747782&method=sha1&sign=pcK%2BV3%2Fql%2BNncCw4w3txNC5nLGA%3D |
请求体:
选择form-data格式
product_id | 产品id |
device_name | 设备名称/设备id |
file | 符合上传格式的图片或文本 |
file上传到平台,可能会先云上传到postman上
fid每次上传都不一样,后面对文件进行操作是通过fid操作的
对文件操作的注意:
都要先在请求头里面添加Authorization鉴权
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。