本文介绍如何调用物联网平台的云端API,很多时候控制台上操作不太方便,需要通过API调用来完成或者通过调用API封装接口,以做到二次开发等。如产品管理,设备管理,规则创建等。分别介绍通过common SDK和iot SDK的使用。
1、 云端API介绍以及调试方法
/document_detail/69893.html?spm=a2c4g.11186623.6.749.1def3112gGOTHP
使用云端API之前务必先使用OpenAPI Explorer在线调试工具先调试通过。
/?spm=a2c4g.11186623.2.12.240a3d29r9yc1o#/?product=Iot&version=-01-20&api=CreateProduct&tab=DEMO&lang=JAVA
其实我们的代码也就是从旁边的示例代码中搬运过来的。
2、使用common SDK调用云端API
前边已经说过,使用OpenAPI Explorer在线调试工具调试好之后,然后将示例代码放到自己的工程中
以创建产品为例:
调试通过,然后就可以放心把代码搬出来了
可以看到控制台上已经有新产品了。
编写代码
测试结果:
控制台上也新建了产品
源码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.example CallAPI 1.0-SNAPSHOT Com.aliyun aliyun-java-sdk-core 4.5.3
CallAPI.Java
package com.alibaba; import monRequest; import monResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; /* pom.xml Com.aliyun aliyun-java-sdk-core 4.5.3 */ public class CallAPI { public static void main(String[] args) {DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4G2aMvPeCrZvJdtXvT3G", "Mp2f4qopmULI6raZS9GEO9ud7rOPGl");IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain("-");request.setSysVersion("-01-20");request.setSysAction("CreateProduct");request.putQueryParameter("RegionId", "cn-shanghai");request.putQueryParameter("NodeType", "0");request.putQueryParameter("ProductName", "ProductAPITestDemo6");request.putQueryParameter("DataFormat", "1");request.putQueryParameter("Description", "测试云端API用");request.putQueryParameter("AliyunCommodityCode", "iothub_senior");request.putQueryParameter("AuthType", "secret");try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData());} catch (ServerException e) { e.printStackTrace();} catch (ClientException e) { e.printStackTrace();} } }
3、使用iot SDK调用云端API
到官网下载Demo
点击链接后,进入Github(没有账户就注册一个)
导入到工程中
同样以创建产品为例,和common SDK一样要找到我们需要修改的参数AK SK等
发现调用到了ProductManager
继续追踪
测试结果:
控制台上创建新产品成功
源码直接去官网下载
注意!注意!!阿里云开发者成长计划正式启动啦,该计划面向全年龄段开发者!!
免费资源、免费体验、免费学习、免费实践 4 大场景,帮助开发者轻松掌握云上技能,助推成长,成就云计算技术人才~
上开发者成长计划的车,一起来学习叭,风里雨里阿里等你!!
传送门在这里:/plan/promotion/1?spm=a1z389.11499242.0.0.65452413zGOcUM&utm_content=g_1000199577
添加阿里妹官微(alimei6)备注【阿里技术】,即可领取Python、Java、数据库、运维等等阿里独家学习资料和福利!票圈还有更多阿里校招社招资讯、免费训练营和大佬干货直播等等等分享!不断更新中~阿里妹这里等你!