如何调用云端API

生活物联网平台提供了多组云端API,您通过云端调用这部分API时,可参照调用示例来完成。

为您提供以下四种语言的云端API调用示例,其他语言请您参照示例中的逻辑和加签机制自行实现。

  • Java调用示例

    // https://github.com/aliyun/iotx-api-gateway-client
    public static void main(String[] args) throws UnsupportedEncodingException {
        IoTApiClientBuilderParams ioTApiClientBuilderParams = 
          new IoTApiClientBuilderParams();
        ioTApiClientBuilderParams.setAppKey("你的<AppKey>");
        ioTApiClientBuilderParams.setAppSecret("你的<AppSecret>");
        SyncApiClient syncClient = new SyncApiClient(ioTApiClientBuilderParams)    ;
    
        IoTApiRequest request = new IoTApiRequest();
        //设置API的版本
        //调用/cloud/token接口时,不需要传CloudToken,请参见获取云端资源Token接口的代码示例
        //调用其他接口(除获取云端资源Token接口外),都需要先调用获取云端资源Token接口,获取到token后,再传入ApiVer和CloudToken,才可以正常调用
        request.setApiVer("1.0.0");
        request.setCloudToken("CloudTokenXXXXX");
    
        //设置接口的参数
        request.putParam("grantType", "project");
        request.putParam("res", "xxxxx"); //授权的资源ID。grantType为project时,res的值为project的ID。
    
        //请求参数域名、path、request
        //host地址  中国站:api.link.aliyun.com     新加坡:ap-southeast-1.api-iot.aliyuncs.com     美国(弗吉尼亚):us-east-1.api-iot.aliyuncs.com     德国(法兰克福):eu-central-1.api-iot.aliyuncs.com
        ApiResponse response = syncClient.postBody("api.link.aliyun.com",
            "/cloud/token", request, true);
        System.out.println( "response code = " + response.getCode() 
                           + " response = " + new String(response.getBody(), "UTF-8"));
    }
                        
  • PHP调用示例

    //https://github.com/aliyun/api-gateway-demo-sign-php (第三方PHP库)
    
    <?php
    include_once 'Util/Autoloader.php';
    
    //host地址:
    //中国站:api.link.aliyun.com
    //新加坡:ap-southeast-1.api-iot.aliyuncs.com 
    //美国(弗吉尼亚):us-east-1.api-iot.aliyuncs.com
    //德国(法兰克福):eu-central-1.api-iot.aliyuncs.com
    function example() {
        $path      = "/cloud/token";
        $host      = "https://api.link.aliyun.com";
        $appKey    = "你的<AppKey>";
        $appSecret = "你的<AppSecret>"; 
    
        $request = new HttpRequest($host, $path, HttpMethod::POST, $appKey, $appSecret);
        //设置API版本和参数,其中,res为授权的资源ID。grantType为project时,res的值为project的ID。
        $body = '{"id":"xxx","version":"1.0","request":{"apiVer":"1.0.0"},' . 
                '"params":{"grantType":"project","res":"xxx"}}';
    
        //设定Content-Type
        $request->setHeader(HttpHeader::HTTP_HEADER_CONTENT_TYPE, 
                            ContentType::CONTENT_TYPE_JSON);
    
        //设定Accept
        $request->setHeader(HttpHeader::HTTP_HEADER_ACCEPT, 
                            ContentType::CONTENT_TYPE_JSON);
    
        if (strlen($body) > 0) {
            $request->setHeader(HttpHeader::HTTP_HEADER_CONTENT_MD5, 
                                base64_encode(md5($body, true)));
            $request->setBodyString($body);
        }
    
        //指定参与签名的header
        $request->setSignHeader(SystemHeader::X_CA_TIMESTAMP);
    
        $response = HttpClient::execute($request);
        var_dump($response);    
    }
    
    example();
                        
  • Python调用示例

    # https://github.com/aliyun/api-gateway-demo-sign-python/
    # -*- coding: utf-8 -*-
    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 = "/cloud/token"
    
    cli = client.DefaultClient(app_key="appKey", app_secret="appSecret")
    
    # construct request
    req_post = request.Request(host=host, protocol=constant.HTTP, url=url, method="POST", time_out=30000)
    body = '{"id":"xxx","version":"1.0","request":{"apiVer":"1.0.0"},"params":{"grantType":"project","res":"xxx"}}'
    req_post.set_body(bytearray(body, encoding="utf8"))
    req_post.set_content_type(constant.CONTENT_TYPE_STREAM)
    print cli.execute(req_post)
  • .NET调用示例

    调用示例请参见调用示例

  • Node.js调用示例

    调用示例请参见调用示例