---
# System prepended metadata

title: Swagger UI (ThingsBoad API) 測試
tags: [ThingsBoard]

---

---
title: Swagger UI (ThingsBoad API) 測試
tags: ThingsBoard
---
# Swagger UI 測試

[後端代理伺服器 (CORS) 解決方法](https://angular.tw/guide/build#proxying-to-a-backend-server)

1. 需先使用login - controller 透過帳號密碼登入並取得JWT Token

    Post controller - http://baseURL:port/api/auth/login
    RequestBody (JSON)

        {
            "username": account,
            "password": password
        }

    以TypeScript編寫，透過jQuery使用ajax為例

        $.ajax({
            url: 'http://baseURL:port/api/auth/login',
            type: 'POST',
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(
                {
                    "username": account,
                    "password": password,
                }
            ),
            dataType: 'json',
            success: (response: any) => {
                this.token = response.token;
            }
            error: () => {}
        }

2. 若身分為User，需使用 user - controller 取得登入帳號之customerId
    GET controller - http://baseURL:port/api/auth/user

        $.ajax({
            url: 'http://baseURL:port/api/auth/user',
            beforeSend:
            (request:{setRequestHeader: (arg0: string, arg1: string) => any;}) =>
            request.setRequestHeader('X-Authorization', 'Bearer ' + this.token),
            type: 'GET',
            success: (response: any) => {
                this.customerId = response.customerId.id;
            }
            error: () => {}
        }

3. 後續需要權限的controller，Request要加上Request Header

        Key: X-Authorization
        Value: JWT Token

    以TypeScript為例

        beforeSend:(request:{setRequestHeader: (arg0: string, arg1: string) => any;}) =>
            request.setRequestHeader('X-Authorization', 'Bearer ' + this.token),

    P.S. login controller 取得的JWT Token並無開頭字串'Bearer '，需自行串接

    否則會出現以下錯誤訊息

        {
          "status": 401,
          "message": "Authentication failed",
          "errorCode": 10,
          "timestamp": "2022-01-07T06:21:40.124+00:00"
        }

* admin-controller (SYS_ADMIN)
    1. 資安規則 C/R/U (含密碼格式、字數限制及連線失敗嘗試次數)
        RequestBody - 

            {
              "passwordPolicy": {
                "minimumDigits": 0,
                "minimumLength": 0,
                "minimumLowercaseLetters": 0,
                "minimumSpecialCharacters": 0,
                "minimumUppercaseLetters": 0,
                "passwordExpirationPeriodDays": 0,
                "passwordReuseFrequencyDays": 0
              },
              "maxFailedLoginAttempts": 0,
              "userLockoutNotificationEmail": "string"
            }
        
    3. POST /api/admin/settings
    4. GET /api/admin/settings/{key} ('general' or 'mail')
    5. POST /api/admin/settings 取得設定物件
    6. GET /api/admin/settings/{key}
    7. POST 發送測試email
    8. POST 發送測試簡訊
    9. GET (檢查平台版本 - 非必要)

* alarm-controller (TENANT_ADMIN or CUSTOMER_USER)
    1. POST /api/alarm - C/U alarm
    2. (GET/DELETE) /api/alarm/{alarmId} R/D alarm by alarmId
    3. POST /api/alarm/{alarmId}/ack [Ack](https://zh.wikipedia.org/wiki/%E7%A2%BA%E8%AA%8D%E8%A8%8A%E6%81%AF)nowledge Alarm
    4. POST /api/alarm/{alarmId}/clear
    5. GET /api/alarm/{entityType}/{entityId}

GET
/api/alarm/highestSeverity/{entityType}/{entityId}{?searchStatus,status}
Get Highest Alarm Severity (getHighestAlarmSeverity)

GET
/api/alarm/info/{alarmId}
Get Alarm Info (getAlarmInfoById)

GET
/api/alarms{?endTime,fetchOriginator,page,pageSize,searchStatus,sortOrder,sortProperty,startTime,status,textSearch}
Get All Alarms (getAllAlarms)

* asset-controller (TENANT_ADMIN or CUSTOMER_USER) - 類似工作站的概念
* audit-log-controller (TENANT_ADMIN) - 紀錄查詢，ex:登入，修改設定
* auth-controller
    1.密碼修改
    2.登出
    3.取得目前登入user資訊
    4.帳號啟動(使用認證碼 | 使用認證信)
    5.重設密碼(使用認證碼 | 使用認證信)
    6.資安設定 (含密碼格式、字數限制及連線失敗嘗試次數)
* component-descriptor-controller (SYS_ADMIN or TENANT_ADMIN)
* customer-controller
* dashboard-controller (TENANT_ADMIN) - 傳回JSON格式
* device-api-controller
* device-controller (TENANT_ADMIN or CUSTOMER_USER)
* device-profile-controller
* edge-controller
* edge-event-controller
* entity-query-controller
* entity-relation-controller
* entity-view-controller
* event-controller
* login-endpoint
* lwm-2m-controller
* o-auth-2-config-template-controller
* o-auth-2-controller
* ota-package-controller
* queue-controller
* rpc-v-1-controller
* rpc-v-2-controller
* rule-chain-controller
* tb-resource-controller
* telemetry-controller
* tenant-controller
* tenant-profile-controller
* ui-settings-controller
* user-controller
* widget-type-controller
* widgets-bundle-controller

## Public Login

* POST - http://192.168.1.202:8081/api/auth/login/public
    Request Body - `{ "publicId": "{public Id}" }`