准备工作
更新时间:2018-10-19 11:29:34
云端服务调用协议
基于安全考虑,所有云端接口的调用仅限于HTTPS方式,不提供HTTP方式的支持。
云端服务调用流程
阿里云IoT云端资源需要云端资源令牌访问。云端资源令牌通过开通云端资源服务获取,云端管理默认开通。
主要流程如下:
获取云端唯一身份AppKey
Java调用示例
// https://github.com/aliyun/iotx-api-gateway-client
public static void main(String[] args) throws UnsupportedEncodingException {
SyncApiClient syncClient = SyncApiClient.newBuilder()
.appKey("你的<AppKey>")
.appSecret("你的<AppSecret>")
.build();
IoTApiRequest request = new IoTApiRequest();
//设置api的版本
request.setApiVer("1.0.0");
request.setCloudToken("CloudTokenXXXXX");
//设置接口的参数
request.putParam("grantType", "project");
request.putParam("res", "xxxxx");
//请求参数域名、path、request
ApiResponse response = syncClient.postBody("api.link.aliyun.com",
"/cloud/token", request, true);
System.out.println(
"response code = " + response.getStatusCode() + " response content = " + new String(response.getBody(),
"utf-8"));
}
PHP调用示例
//https://github.com/aliyun/api-gateway-demo-sign-php (第三方php库)
<?php
include_once 'Util/Autoloader.php';
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版本和参数
$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();