当前位置:网站首页>[graffiti Internet of things footprint] graffiti cloud platform interface description

[graffiti Internet of things footprint] graffiti cloud platform interface description

2020-11-09 17:44:00 IOT cloud workshop

_ Preface series of articles >>> 【 Doodling the footprints of the Internet of things 】API And SDK Introduce

Our series of articles , It's all about how to complete an intelligence “ Space kissing machine ” Development of . I hope it can help the long-distance love or Exotic lovers !

In this article, we will explain in general based on OpenAPI What's involved in development is API Calling method 、 standard 、 Examples and integration SDK Related content . The content will be hard core , Please pay attention to !

One 、 Development process

  1. register Developer account .
  2. Cloud development creates cloud application projects , obtain  client_id & secret.

explain : Developer Platform key For the name of the  accessId & accessKey.

  1. establish SDK: stay Doodle IoT platform , choice  App The workbench  > App SDK >  obtain SDK >  Fill in the parameters as required  >  obtain schema( Channel logo ).
  2. be based on OpenAPI Business development .
  3. After the test is correct , Developers release themselves .

Two 、 Authorization process

Every business OpenAPI All need to be done token check .

explain : Doodle OpenAPI follow OAuth 2.0 Protocol standard .

3、 ... and 、 Simple mode

For the cloud cloud docking scenario , Graffiti provides an implicit way to obtain :

  1. According to the graffiti cloud OpenAPI Interface specification for developers  client_id  and  secret  Do signature verification .
  2. Graffiti cloud verifies and issues token to third party cloud .

explain : Obtained by implicit authorization token, The permission dimension is the developer dimension ,token The operation permission range of the developer is the scope of the permission Operation of the developer , Such as operating ( increase 、 Delete 、 Change 、 check ) Developer's application user data , The device data under the product and the device data bound by the user under the application .

Four 、 The interface specification

Environmental statement

Each user of the interface should call the corresponding interface according to its own region .

China proper https://openapi.tuyacn.com

Americas https://openapi.tuyaus.com

Europe https://openapi.tuyaeu.com

India https://openapi.tuyain.com

Request mode

Support can be requested in the following way :

  • GET
  • PUT
  • POST
  • DELETE

explain : When the request mode is POST when ,Content-Type  Need to use application/json.

Request header Settings

Any interface needs to be in header Add the following parameters to :

explain : Business interface ( Not token Interface ) The request requires parameters access_token.

5、 ... and 、 Signature specification

Graffiti cloud uses HMAC-SHA256 Create a summary , According to different application scenarios , Two signature algorithms are currently available :

  • Token management interface ( Get token 、 Refresh token )

sign = HMAC-SHA256(client_id + t, secret).toUpperCase()

Use the applied client_id With the current request 13 Bit standard timestamps are spliced into strings to be signed , Use the applied Cloud Application secret Participate in hash digest as key , The resulting string , Finally, capitalize ;

  • Business interface

sign = HMAC-SHA256(client_id + access_token + t, secret).toUpperCase()

Use the applied cloud app client_id + Currently valid request token + Currently requested 13 Bit standard timestamps are spliced into strings to be signed , Use the applied Cloud Application secret Participate in hash digest as key , The resulting string , Finally, capitalize .

  • Signature example
  1. Prepare parameters :

client_id:1KAD46OrT9HafiKdsXeg

secret:4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC

t:1588925778000

access_token:3f4eda2bdec17232f67c0b188af3eec1

  1. Token management interface signature :

String to be signed :1KAD46OrT9HafiKdsXeg1588925778000

Signature result :HMAC-SHA256(1KAD46OrT9HafiKdsXeg1588925778000,4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC)

ceaafb5ccdc2f723a9fd3e91d3d2238ee0dd9a6d7c3c365deb50fc2af277aa83

Put it in capitals :CEAAFB5CCDC2F723A9FD3E91D3D2238EE0DD9A6D7C3C365DEB50FC2AF277AA83

3 Business interface :

String to be signed :1KAD46OrT9HafiKdsXeg3f4eda2bdec17232f67c0b188af3eec11588925778000

Signature result :HMAC-SHA256(1KAD46OrT9HafiKdsXeg3f4eda2bdec17232f67c0b188af3eec11588925778000,4OHBOnWOqaEC1mWXOpVL3yV50s0qGSRC)

36c30e300f226b68add014dd1ef56a81edb7b7a817840485769b9d6c96d0faa1

Put it in capitals :36C30E300F226B68ADD014DD1EF56A81EDB7B7A817840485769B9D6C96D0FAA1

4 All kinds of languages HMAC SHA256 The implementation of the :

Javascript HMAC SHA256

/**
Run the code online with this jsfiddle. Dependent upon an open source js library calledhttp://code.google.com/p/crypto-js/.
**/

<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>

<script> var hash = CryptoJS.HmacSHA256("Message", "secret");
  var hashInBase64 = hash.toString().toUpperCase();
  document.write(hashInBase64); </script>

PHP HMAC SHA256

/**
PHP has built in methods for hash_hmac (PHP 5) and base64_encode (PHP 4, PHP 5) resulting in no outside dependencies. Say what you want about PHP but they have the cleanest code for this example.
**/

$s = hash_hmac('sha256', 'Message', 'secret', true);
echo strtoupper(var_dump($s));

Java HMAC SHA256

/**
Dependent on Apache Commons Codec to encode in base64.
**/

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class ApiSecurityExample {
  public static void main(String[] args) {
    try {
     String secret = "secret";
     String message = "Message";

     Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
     SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
     sha256_HMAC.init(secret_key);

      byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
     String hash = new HexBinaryAdapter().marshal(bytes).toUpperCase();
     System.out.println(hash);
    }
    catch (Exception e){
     System.out.println("Error");
    }
   }
}

 C# HMAC SHA256

using System;
using System.Security.Cryptography;

namespace Test
{
  public class MyHmac
  {
    public static string Encrypt(string message, string secret)
            {
                secret = secret ?? "";
                var encoding = new System.Text.UTF8Encoding();
                byte[] keyByte = encoding.GetBytes(secret);
                byte[] messageBytes = encoding.GetBytes(message);
                using (var hmacsha256 = new HMACSHA256(keyByte))
                {
                    byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < hashmessage.Length; i++)
                    {
                        builder.Append(hashmessage[i].ToString("x2"));
                    }
                    return builder.ToString().ToUpper();
                }
            }
  }
} 

Return results United return JSON, The general format is as follows :

The request is successful

{
    "success": true,
    "result": {
        //object
    }
}

Request exception

{
    "success": false,
    "code": 1010,
    "msg": "token illegal "
}

6、 ... and 、 Integrate SDK Java summary

Currently available based on Java Of Tuya Cloud SDK Encapsulates the token relevant 、 User related and device related interfaces , In order to accelerate the development of cloud cloud docking .

Developers only need to pay attention to the call of business function methods used , Build the corresponding TuyaClient example , The instance will be updated automatically token And complete the correspondence API Call to .SDK It mainly includes the following functions , For detailed interface information, please refer to the corresponding module later :

token relevant ( No user calls are required )

User correlation ( Get the list of users 、 Registered users 、 Get the device list under the user )

Equipment related ( Get the equipment distribution network token、 Get the distribution network token Under the list of all devices and other interfaces )

Integrate SDK

IDEA Import jar package : https://jingyan.baidu.com/article/0f5fb0993e9e1f6d8334ead2.html

Eclipse Import jar package : https://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html

GitHub Address

https://github.com/TuyaInc/tuya_cloud_sdk_java

General module

Because some new interfaces cannot be synchronized and integrated to SDK, Developers can use SDK The general interface is extended horizontally to meet the needs of development .

obtain Header list :

/**
 *  obtain Header list 
 * @param isToken  Whether it is token Relevant request , It's usually false
 * @return
 */
public List<Header> getHeaders(Boolean isToken)

Universal graffiti interface :

/**
 *  Universal graffiti interface 
 * @param url
 * @param method  Request type ( for example :GET)
 * @param headers  Request header content ( Extra new header)
 * @param body
 * @return
 */
public String commonHttpRequest(String url, HttpMethod method, Map<String, String> headers, Object body)

Invoke the sample

Here is an example of a registered user :

​
TuyaClient client = new TuyaClient(clientId, secret, RegionEnum.CN);
String uid = client.registerUser("testApp","86","18212345678", MD5Util.getMD5("123456")"nickName",UserTypeEnum.MOBLIE);
System.out.println(" Successfully synchronized user : "+ uid);

​

Golang

Golang SDK Source code address , Please see the Golang SDK.

版权声明
本文为[IOT cloud workshop]所创,转载请带上原文链接,感谢