调用接口示例

数字签名

API使用摘要签名认证方式(APP Key和APP Secret), 客户端在调用API时,需要使用签名密钥对请求内容进行签名计算, 并将签名同步传输给服务器端进行签名验证。API网关提供的SDK内置了签名实现, 您只需要将签名密钥配置在SDK中。

登录工业数据应用平台,选择左边菜单栏“应用”-“应用授权”,“创建AppKey/AppSecret”或者使用已有的AppKey/AppSecret。image.png

Java-SDK

SDK地址: https://github.com/aliyun/alibabacloud-iot-api-gateway-sdk

Maven依赖:

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>iotapigateway</artifactId>
            <version>0.0.1</version>
        </dependency>

示例:描述如何通过调用/industry/asset/openapi/user/add添加账号。

import com.aliyun.iotapigateway.Client;
import com.aliyun.iotapigateway.models.CommonParams;
import com.aliyun.iotapigateway.models.Config;
import com.aliyun.iotapigateway.models.IoTApiRequest;
import com.aliyun.tea.TeaResponse;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.HashMap;
import java.util.Map;

public class ClientTest {
    //服务域名/ip,端口固定是30101
    private static final String HOST = "192.168.1.100:30101";
    //服务路径
    private static final String PATH = "/industry/asset/openapi/user/add";
    //服务版本,目前默认1.0.0
    private static final String VERSION = "1.0.0";
    private static final String HTTP_COL = "HTTP";
    private static final String HTTP_METHOD = "POST";

    public static void main(String[] args) throws Exception{
        Config config = new Config();
        //配置环境变量 export IDAP_APP_KEY = "xxxx"
        String ak = System.getenv("IDAP_APP_KEY");
        //配置环境变量 export IDAP_APP_SECRET = "xxxx"
        String as = System.getenv("IDAP_APP_SECRET");
        config.appKey = ak;
        config.appSecret = as;
        config.domain = HOST;
        Client client = new Client(config);
        Map<String, Object> params = new HashMap<>();

        params.put("loginName", "alex");
        params.put("nickName", "张先生");
        params.put("phone", "138xxxxxxxx");

        CommonParams req = new CommonParams();
        req.apiVer = VERSION;

        IoTApiRequest body = new IoTApiRequest();
        body.params = params;
        body.request = req;

        RuntimeOptions runtime = new RuntimeOptions();

        TeaResponse teaResponse = client.doRequest(PATH, HTTP_COL, HTTP_METHOD, null, body, runtime);
        //请求返回, 解析出对象 Common支持多种转换读取根据实际转换处理
        Object obj = Common.readAsJSON(teaResponse.body);
        Map<String, Object> result = Common.assertAsMap(obj);
    }
}