API调用对接指引

更新时间:

应用调用平台API时,需要对请求做加签处理,为了简化应用开发,平台提供了SDK。

应用调用平台API时,需要对请求做加签处理,为了简化应用开发,平台提供了SDK。

1 API调用SDK下载

开发语言

sdk demo地址

Java

https://github.com/aliyun/iotx-api-gateway-client

python

https://github.com/aliyun/api-gateway-demo-sign-python

php

https://github.com/aliyun/api-gateway-demo-sign-php

c#

https://github.com/aliyun/api-gateway-demo-sign-net

android

https://github.com/aliyun/api-gateway-demo-sign-android

2 API调用示例

2.1 SDK介绍

2.1.1 Java SDK

依赖

<dependency>
  <groupId>com.aliyun.api.gateway</groupId>
  <artifactId>sdk-core-java</artifactId>
  <version>1.0.4</version>
</dependency>

示例代码

 SyncApiClient syncClient = SyncApiClient.newBuilder()
                .appKey("1234567")
                .appSecret("6726732dsfdsdsfdsfds")
                .build();
 IoTApiRequest request = new IoTApiRequest();
 //设置api的版本
 request.setApiVer("0.1.0");                
 // 接口参数
 request.putParam("json","{}");
 request.putParam("productKey","a1ILGykZVhP");
 request.putParam("deviceName","Dxb3lv1E78mg5dsVLUQW");
 request.putParam("serviceName","kkp");
 //请求参数域名、path、request
 ApiResponse response = syncClient.postBody("test123-cn-shanghai.alicloudapi.com",
                "/invokeThingService", request);
 System.out.println("response code = " + response.getStatusCode() + " response content = " + new String(response.getBody(),
                        "utf-8"));
 }

2.2.2 Python SDK

  1. 示例工程基于Python 2.7,Python3请自行适配。

git clone https://github.com/aliyun/api-gateway-demo-sign-python.git

2.修改signature_composer.py文件第49行。

修改前:string_to_sign.append(_build_resource(uri=uri, body=body))

修改后:string_to_sign.append(uri)

3.修改ClientDemo.py如下,执行调用。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import json

sys_path = sys.path[0]
if not os.path.isdir(sys_path):
    sys_path = os.path.dirname(sys_path) 
os.chdir(sys_path)

from com.aliyun.api.gateway.sdk import client
from com.aliyun.api.gateway.sdk.http import request
from com.aliyun.api.gateway.sdk.common import constant

host = "https://api.link.aliyun.com"
url = "/data/model/data/query"
cli = client.DefaultClient(app_key="1234567", app_secret="abcdefghijklmnopq")
req_post = request.Request(host=host, protocol=constant.HTTPS, url=url, method="POST", time_out=30000)
req_post.set_content_type(constant.CONTENT_TYPE_STREAM)

body = {
    "request":{
        "apiVer":"0.0.2"
    },
    "params":{
        "modelId":"EMPLOYEE",
        "returnFields":["employee_no", "name"],
        "conditions":[{"fieldName":"id","value":"1","operate":"mt"}],
        "pageSize": 100,
        "pageNum": 1
    },
    "version":"1.0"
}
bb = json.dumps(body).encode("utf-8")

req_post.set_body(bb)

print '==================================================='
print str(cli.execute(req_post)).decode('string_escape')
print '==================================================='