Skip to content

Instantly share code, notes, and snippets.

@nklamann
Last active November 21, 2024 12:10
Show Gist options
  • Save nklamann/dbb4a859eae3dde15727de3526587181 to your computer and use it in GitHub Desktop.
Save nklamann/dbb4a859eae3dde15727de3526587181 to your computer and use it in GitHub Desktop.
OMADA openapi calls demonstration

Motivation

This is a variation of this omada example, transcribed to the new openapi. The API documentation of the softwarecontroller is in https://<omadaserver>:8043/doc.html#/home.

Scope

I wnated to test and document the uage of the new API in itself, especially the handling of the Authorization. Working with specific endpoints is ut of scope here.

Result of omada.bash

I left a lot of echo and cat statements in the code to better see what is happening. Running this on my machine i get:

{
  "errorCode": 0,
  "msg": "Open API Log in successfully.",
  "result": {
    "csrfToken": "01ee52939f8f4a739304a8c3df021a8b",
    "sessionId": "d6bfeebe5df546b38cf1455f39cd71ba"
  }
}
CSRFTOKEN=01ee52939f8f4a739304a8c3df021a8b
SESSIONID=d6bfeebe5df546b38cf1455f39cd71ba
{
  "errorCode": 0,
  "msg": "Open API Authorize successfully.",
  "result": "OC-IAPAFH1wtx5fkZCfNNtKscnDRCYlfcXt"
}
AUTHCODE=OC-IAPAFH1wtx5fkZCfNNtKscnDRCYlfcXt
{
  "errorCode": 0,
  "msg": "Open API Get Access Token successfully.",
  "result": {
    "accessToken": "AT-uBtS3HaO3B6sFa7ajMIKnTJLwaecYzQS",
    "tokenType": "bearer",
    "expiresIn": 7200,
    "refreshToken": "RT-VhdGW51kbqLjS5aCd2PZEuCnLWtbdGC8"
  }
}
ACCESSTOKEN=AT-uBtS3HaO3B6sFa7ajMIKnTJLwaecYzQS
REFRESHTOKEN=RT-VhdGW51kbqLjS5aCd2PZEuCnLWtbdGC8
{
  "errorCode": 0,
  "msg": "Success.",
  "result": {
    "totalRows": 1,
    "currentPage": 1,
    "currentSize": 1,
    "data": [
      {
        "siteId": "660293f1ae766909a69251d4",
        "name": "FM174",
        "region": "Germany",
        "timeZone": "Europe/Amsterdam",
        "scenario": "Home",
        "type": 0
      }
    ]
  }
}
### Bash Example
# set variables
OMADA_URL="https://omada:8043"
USERNAME="Viewer"
PASSWORD="grundlos"
CLIENT_ID="fb355d589ebf4b798415ca612d99819a" # from application list
CLIENT_SECRET="1c369a99a1574b5d8ed959c6e88b9565" # from application list
OMADA_ID="c491b4a4d05ff0d58282f4645848e9ec" # from view open api attributes
curl -s "${OMADA_URL}/openapi/authorize/login?client_id=${CLIENT_ID}&omadac_id=${OMADA_ID}" \
-H 'content-type:application/json' -d "{\"username\":\"${USERNAME}\",\"password\":\"${PASSWORD}\"}" -k --insecure > /tmp/login.json
CSRFTOKEN=$(cat /tmp/login.json | jq -r .result.csrfToken )
SESSIONID=$(cat /tmp/login.json | jq -r .result.sessionId )
cat /tmp/login.json | jq
echo "CSRFTOKEN=${CSRFTOKEN}"
echo "SESSIONID=${SESSIONID}"
curl -s "${OMADA_URL}/openapi/authorize/code?client_id=${CLIENT_ID}&omadac_id=${OMADA_ID}&response_type=code" \
-H 'content-type:application/json' -H "Csrf-Token:${CSRFTOKEN}" -H "Cookie:TPOMADA_SESSIONID=${SESSIONID}" -X POST -k --insecure > /tmp/authcode.json
AUTHCODE=$(cat /tmp/authcode.json | jq -r .result)
cat /tmp/authcode.json | jq
echo "AUTHCODE=${AUTHCODE}"
curl -s "${OMADA_URL}/openapi/authorize/token?grant_type=authorization_code&code=${AUTHCODE}" \
-H 'content-type:application/json' -d "{\"client_id\": \"${CLIENT_ID}\", \"client_secret\": \"${CLIENT_SECRET}\"}" -X POST -k --insecure > /tmp/token.json
ACCESSTOKEN=$(cat /tmp/token.json | jq -r .result.accessToken )
REFRESHTOKEN=$(cat /tmp/token.json | jq -r .result.refreshToken )
cat /tmp/token.json | jq
echo "ACCESSTOKEN=${ACCESSTOKEN}"
echo "REFRESHTOKEN=${REFRESHTOKEN}"
## example
curl -s "${OMADA_URL}/openapi/v1/${OMADA_ID}/sites?pageSize=1&page=1" -H 'content-type:application/json' -H "Authorization:AccessToken=${ACCESSTOKEN}" -X GET -k --insecure | jq
@bordzee
Copy link

bordzee commented Jun 14, 2024

Theres no voucher or hotspot endpoints

@ttocsr
Copy link

ttocsr commented Nov 18, 2024

I was trying to get wireguard peer information but had no luck.

  "errorCode": -1005,
  "msg": "Operation forbidden."

/openapi/v1/{omadacId}/sites/{siteId}/vpn/wireguard-peers
Have you been able to get anything more than the site id out of this?

@nklamann
Copy link
Author

Sorry @bordzee and @ttocsr : I did not test specific endpoints here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment