全部产品
云市场

API调用对接指引

更新时间:2019-11-22 13:14:12

应用调用平台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 SDK介绍

2.1 Java SDK
依赖
  1. <dependency>
  2. <groupId>com.aliyun.api.gateway</groupId>
  3. <artifactId>sdk-core-java</artifactId>
  4. <version>1.0.4</version>
  5. </dependency>

示例代码
  1. SyncApiClient syncClient = SyncApiClient.newBuilder()
  2. .appKey("1234567")
  3. .appSecret("6726732dsfdsdsfdsfds")
  4. .build();
  5. IoTApiRequest request = new IoTApiRequest();
  6. //设置api的版本
  7. request.setApiVer("0.1.0");
  8. // 接口参数
  9. request.putParam("json","{}");
  10. request.putParam("productKey","a1ILGykZVhP");
  11. request.putParam("deviceName","Dxb3lv1E78mg5dsVLUQW");
  12. request.putParam("serviceName","kkp");
  13. //请求参数域名、path、request
  14. ApiResponse response = syncClient.postBody("test123-cn-shanghai.alicloudapi.com",
  15. "/invokeThingService", request);
  16. System.out.println("response code = " + response.getStatusCode() + " response content = " + new String(response.getBody(),
  17. "utf-8"));
  18. }

2.2 Python SDK

依赖

Step 1: 下载示例工程代码。示例工程在基于Python 2.7,Python3请自行适配。

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

Step 2:
修改signature_composer.py文件第49行

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

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

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import json
  6. sys_path = sys.path[0]
  7. if not os.path.isdir(sys_path):
  8. sys_path = os.path.dirname(sys_path)
  9. os.chdir(sys_path)
  10. from com.aliyun.api.gateway.sdk import client
  11. from com.aliyun.api.gateway.sdk.http import request
  12. from com.aliyun.api.gateway.sdk.common import constant
  13. host = "https://api.link.aliyun.com"
  14. url = "/data/model/data/query"
  15. cli = client.DefaultClient(app_key="1234567", app_secret="abcdefghijklmnopq")
  16. req_post = request.Request(host=host, protocol=constant.HTTPS, url=url, method="POST", time_out=30000)
  17. req_post.set_content_type(constant.CONTENT_TYPE_STREAM)
  18. body = {
  19. "request":{
  20. "apiVer":"0.0.2"
  21. },
  22. "params":{
  23. "modelId":"EMPLOYEE",
  24. "returnFields":["employee_no", "name"],
  25. "conditions":[{"fieldName":"id","value":"1","operate":"mt"}],
  26. "pageSize": 100,
  27. "pageNum": 1
  28. },
  29. "version":"1.0"
  30. }
  31. bb = json.dumps(body).encode("utf-8")
  32. req_post.set_body(bb)
  33. print '==================================================='
  34. print str(cli.execute(req_post)).decode('string_escape')
  35. print '==================================================='