API problem

API problem

API problem
API problem
2024-06-16 20:18:32
Tags: #API
Model: OC200  
Hardware Version: V1
Firmware Version: 5.13

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&currentPageSize=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&currentPageSize=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

  0      
  0      
#1
Options
2 Reply
Re:API problem
2024-06-17 14:46:02
Thread bump PS: i also added a get function on post for getting the dashboard tabs and get dashboard without tabs
  0  
  0  
#2
Options
Re:API problem
2024-06-18 02:10:53

Hi  @wickedcool13 

 

Thank you so much for taking the time to post the issue on the TP-Link community!

To better assist you, I've created a support ticket via your registered email address, and escalated it to our support engineer to look into the issue. The ticket ID is TKID240632350, please check your email box and ensure the support email is well received. Thanks!

Once the issue is addressed or resolved, welcome to update this topic thread with your solution to help others who may encounter the same issue as you did.

Many thanks for your great cooperation and patience!

 

>> Omada EAP Firmware Trial Available Here << *Try filtering posts on each forum by Label of [Early Access]*
  0  
  0  
#3
Options

Information

Helpful: 0

Views: 473

Replies: 2

Tags

Related Articles