API problem
I tried to get the list of the vouchers using api. Im using postman to test it out.
Ive added post login, auth code, access token, and get voucher. I test it out from login to get voucher code. The problem is the get voucher result.
The code goes on
i was expecting to have like this result: (im using powershell to double check and it works)
on this powershell code:
this is a short version of the code
# Request: Get vouchers
try {
$vouchersUrl = "$OMADA_URL/$OMADA_ID/api/v2/hotspot/sites/$SITE_ID/vouchers"
$vouchersHeaders = @{
"Content-Type" = "application/json"
"Authorization" = "AccessToken=$ACCESSTOKEN"
}
$vouchersResponse = Invoke-RestMethod -Uri $vouchersUrl -Method Get -Headers $vouchersHeaders -WebSession $session -UseBasicParsing
Write-Output ($vouchersResponse | ConvertTo-Json -Depth 10)
} catch {
Write-Error "Failed to get vouchers: $_"
}
Im also confuse on this path using inspect element > network tab under the hotspot page
https://my-ip:port/omadadevice/api/v2/hotspot/sites/siteid/voucherGroups?currentPage=1¤tPageSize=10&searchField=name%2Ccode&_=1718552207755
the api/v2/hotspot/sites/$SITE_ID/vouchers will display the vouchergroups using powershell but on the postman it will show an html code
and if use the api/v2/hotspot/sites/$SITE_ID/voucherGroups on powershell, it will work but on postman its shows html
This is my powershell code
# Ignore SSL warnings (not recommended for production environments)
if (-not ("TrustAllCertsPolicy" -as [type])) {
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}
# Set variables
$OMADA_URL = "https://ip:port"
$USERNAME = "user"
$PASSWORD = "pass"
$CLIENT_ID = "client id" # from application list
$CLIENT_SECRET = "client secret" # from application list
$OMADA_ID = "omada id" # from view open api attributes
$SITE_ID = "site id" # replace with your actual site ID
# Create a web session object
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
# Login and obtain CSRF token and session ID
try {
$loginUrl = "$OMADA_URL/openapi/authorize/login?client_id=$CLIENT_ID&omadac_id=$OMADA_ID"
$loginBody = @{
username = $USERNAME
password = $PASSWORD
} | ConvertTo-Json
$loginResponse = Invoke-RestMethod -Uri $loginUrl -Method Post -Body $loginBody -ContentType "application/json" -WebSession $session -UseBasicParsing
$CSRFTOKEN = $loginResponse.result.csrfToken
$SESSIONID = $loginResponse.result.sessionId
Write-Output "CSRFTOKEN=$CSRFTOKEN"
Write-Output "SESSIONID=$SESSIONID"
} catch {
Write-Error "Failed to login: $_"
exit
}
# Obtain authorization code
try {
$authCodeUrl = "$OMADA_URL/openapi/authorize/code?client_id=$CLIENT_ID&omadac_id=$OMADA_ID&response_type=code"
$authHeaders = @{
"Content-Type" = "application/json"
"Csrf-Token" = $CSRFTOKEN
"Cookie" = "TPOMADA_SESSIONID=$SESSIONID"
}
$authCodeResponse = Invoke-RestMethod -Uri $authCodeUrl -Method Post -Headers $authHeaders -WebSession $session -UseBasicParsing
$AUTHCODE = $authCodeResponse.result
Write-Output "AUTHCODE=$AUTHCODE"
} catch {
Write-Error "Failed to obtain authorization code: $_"
exit
}
# Obtain access token
try {
$tokenUrl = "$OMADA_URL/openapi/authorize/token?grant_type=authorization_code&code=$AUTHCODE"
$tokenBody = @{
client_id = $CLIENT_ID
client_secret = $CLIENT_SECRET
} | ConvertTo-Json
$tokenResponse = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $tokenBody -ContentType "application/json" -WebSession $session -UseBasicParsing
$ACCESSTOKEN = $tokenResponse.result.accessToken
$REFRESHTOKEN = $tokenResponse.result.refreshToken
Write-Output "ACCESSTOKEN=$ACCESSTOKEN"
Write-Output "REFRESHTOKEN=$REFRESHTOKEN"
} catch {
Write-Error "Failed to obtain access token: $_"
exit
}
# Request: Get vouchers
try {
$vouchersUrl = "$OMADA_URL/$OMADA_ID/api/v2/hotspot/sites/$SITE_ID/voucherGroups?currentPage=1¤tPageSize=20"
$vouchersHeaders = @{
"Content-Type" = "application/json"
"Authorization" = "AccessToken=$ACCESSTOKEN"
}
$vouchersResponse = Invoke-RestMethod -Uri $vouchersUrl -Method Get -Headers $vouchersHeaders -WebSession $session -UseBasicParsing
Write-Output ($vouchersResponse | ConvertTo-Json -Depth 10)
} catch {
Write-Error "Failed to get vouchers: $_"
}
# Request: Get dashboard tabs
try {
$dashboardTabsUrl = "$OMADA_URL/openapi/v1/$OMADA_ID/sites/$SITE_ID/dashboard/tabs"
$dashboardTabsHeaders = @{
"Content-Type" = "application/json"
"Authorization" = "AccessToken=$ACCESSTOKEN"
}
$dashboardTabsResponse = Invoke-RestMethod -Uri $dashboardTabsUrl -Method Get -Headers $dashboardTabsHeaders -WebSession $session -UseBasicParsing
Write-Output ($dashboardTabsResponse | ConvertTo-Json -Depth 10)
} catch {
Write-Error "Failed to get dashboard tabs: $_"
}
#The voucher wont work if theres no dashboard
#Added dashboards
# Request: Get dashboard without overall tabs
try {
$dashboardWithoutOverallTabsUrl = "$OMADA_URL/openapi/v1/$OMADA_ID/sites/$SITE_ID/dashboard/without-overall-tabs"
$dashboardWithoutOverallTabsHeaders = @{
"Content-Type" = "application/json"
"Authorization" = "AccessToken=$ACCESSTOKEN"
}
$dashboardWithoutOverallTabsResponse = Invoke-RestMethod -Uri $dashboardWithoutOverallTabsUrl -Method Get -Headers $dashboardWithoutOverallTabsHeaders -WebSession $session -UseBasicParsing
Write-Output ($dashboardWithoutOverallTabsResponse | ConvertTo-Json -Depth 10)
} catch {
Write-Error "Failed to get dashboard without overall tabs: $_"
}
Please help me out. We need a proper full api documentation