REST API 概述

最新更新:2024-07-10

基本約束

  • API 被設計為符合 HTTP、 REST 規範。例如:查詢請求使用 Get 方法,提交請求使用 POST 方法。如果一個請求不是相應的 HTTP 方法,將返回錯誤。
  • 如無特殊說明,調用參數值應轉碼為:UTF-8, URL 編碼

數據中心及Base URL

數據中心 Base URL
新加坡 https://webpush.api.engagelab.cc/v4
中國香港 https://webpush-hk.api.engagelab.cc/v4

當使用REST API時,需要將選定的base URL與具體API的路徑結合起來獲得完整的調用地址。

{base URL}/{API路徑}
          {base URL}/{API路徑}

        
此代碼塊在浮窗中顯示

其中,{base URL}是選定的數據中心的base URL,{API路徑}是具體API的路徑,如 v4/devices/{registration_id}

示例:

  • 假設選擇了新加坡數據中心,其base URL為 https://webpush.api.engagelab.cc
  • 如果要刪除特定設備的註冊信息,API路徑是 v4/devices/{registration_id},則完整的API調用地址為:
https://webpush.api.engagelab.cc/v4/devices/{registration_id}
          https://webpush.api.engagelab.cc/v4/devices/{registration_id}

        
此代碼塊在浮窗中顯示

其中, {registration_id} 是具體設備的註冊ID。

通過這種方法,您可以構建出用於訪問不同數據中心的完整的API調用地址。

鑒權方式

Engagelab REST API 採用 HTTP 基本認證 的驗證方式。 基本做法為,HTTP Header(頭)裏加 Authorization:

Authorization: Basic base64_auth_string
          Authorization: Basic base64_auth_string

        
此代碼塊在浮窗中顯示

其中 base64_auth_String 的生成算法為:base64(appKey:masterSecret)
即,對 appKey 加上冒號,加上 masterSecret 拚裝起來的字符串,再做 base64 轉換。
進入【應用程式設定】-【應用程式資訊】頁面獲取 appKey 和 masterSecret 參數。
image.png

鑒權舉例

你的 appKey 是 "c96f42e0d2e662e45d035ab1", masterSecret 是 "df4d59e84eac2f9d53b36f12",則調用 Push API v4 時,使用 curl 命令調用如下:

curl --insecure -X POST -v https://webpush.api.engagelab.cc/v4/push -H "Content-Type: application/json" -u "c96f42e0d2e662e45d035ab1:df4d59e84eac2f9d53b36f12" -d '{ "from": "push", "to": "all", "body": { "platform": "web", "notification": { "alert": "Hi,MTPush !", "web": { "alert": "Hi,MTPush !", "title": "web_push", "url": "http://www.google.com", "extras": { "web-key1": "web-value1" } } } }, "request_id": "12345678", "custom_args": "business info" }'
           curl --insecure -X POST -v https://webpush.api.engagelab.cc/v4/push 
-H "Content-Type: application/json" 
-u "c96f42e0d2e662e45d035ab1:df4d59e84eac2f9d53b36f12" 
-d '{
    "from": "push",
    "to": "all",
    "body": {
        "platform": "web",
        "notification": {
            "alert": "Hi,MTPush !",
            "web": {
                "alert": "Hi,MTPush !",
                "title": "web_push",
                "url": "http://www.google.com",
                "extras": {
                    "web-key1": "web-value1"
                }
            }
        }
    },
    "request_id": "12345678",
    "custom_args": "business info"
}'

        
此代碼塊在浮窗中顯示

HTTP 請求發出的請求是:

> POST /v4/push HTTP/1.1 > Authorization: Basic Yzk2ZjQyZTBkMmU2NjJlNDVkMDM1YWIxOmRmNGQ1OWU4NGVhYzJmOWQ1M2IzNmYxMg==
          > POST /v4/push HTTP/1.1
> Authorization: Basic Yzk2ZjQyZTBkMmU2NjJlNDVkMDM1YWIxOmRmNGQ1OWU4NGVhYzJmOWQ1M2IzNmYxMg==

        
此代碼塊在浮窗中顯示
在文档中心打开
Contact Sales