How to authorize to Horizon Server API Swagger UI

A while back a dedicated UI was added to Horizon Connection Server to help administrators browse through available REST API calls and even execute them. You can find the Swagger UI using this URL on every Connection Server:

https://connection_server_url/rest/swagger-ui.html

It’s easy to access and browse as a documentation but might be a challange to execute your calls.

As a first step, you need to authenticate to the REST API with your Horizon Administrator login credentials. You do this by executing this curl command:

Once authenticated, you should receive as part of a response an Access Token:

You need to copy the access_token data (everything between quotation marks but without those marks) and use it to constuct such string:

Bearer eyJhbGciOiJSUzI1NiJ9.eyJ1c2VyLXNpZCI6IlMtMS01LTIxLTM2Nzc0Mjk3NS0zNDM1OTQ3NDI2LTMyMzExNTQzNzEtMTExNSIsImNsb3VkQWRtaW4iOmZhbHNlLCJicm9rZXIgaWQiOiJIQ1MxIiwidG9rZW4gdHlwZSI6ImFjY2VzcyB0b2tlbiIsInN1YmplY3QiOiJ3c2FkbWluIiwiZG9tYWluIjoidmRlbW8ucGwiLCJkb21haW4tdHJ1c3QtdHlwZSI6IlBSSU1BUllfRE9NQUlOIiwic2Vzc2lvbi1pZCI6ImJiMzJlNzdwLTFmM2QtNDRmNC1iZmVmLWVhZTkxZjUyMzQzMSIsInN1YiI6IndzYWRtaW4iLCJpYXQiOcE2NDIyNTY2OTYsImp0aSI6IjYwOTQyZGI0LWI1NWQtNDRlZC1iZjYxLWEwYWI2YzQwMWRlMyIsIm5iZiI6MTY0MjI1NjY5NiwiZXhwIjoxNjQyMjU4NDk2fQ.Xvp6XPgGHeyExfGcVXbXNV8bt2EF9-CGunPAaTjXTwG8xod8v_5RPd5BFfpJ8UNRZ7cNM1WBUdf8LYuhbM-zThqfT5kO65eR35j1Vn2PzJr6A_YzmV0viiWJQHzWXm_HNOLFN_7q9_gdp4WHjlMAYtUx34R1DiLCpEdTb-G4eessjXmN3J2C6QTUsP5hHKjgyzKfP7h6D6OWF7hUWfqTQHqElO3A5I2sCg7UbxRD3dD9DPFREop5fyp0AbNwn7AnN5aDm8gbmbmEvXVGspxbhLr_AmlTO1VuWAhQoUAm7cC1YgGDoa2GObosIbqnogzZMZf-G_-Sc9taT9HxW-1HBA

Next, you paste this string as an apiKey into the Authorization dialog box:

As you can notice, the “Bearer” in front of the Access Token is the trick to get authorised successfully.

Once you do that, you can start playing with REST API calls.

If you plan to use swagger frequently, you can find the script below that will put the proper string with Access Token in your clipboard so you can paste it quickly. Script author: https://www.retouw.nl/

$url = read-host "url for connectionserver"
$username=read-host "Username"
$domain=Read-host "Domain"
$password=read-host "Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) 
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
function Get-HRHeader(){
    param($accessToken)
    return @{
        'Authorization' = 'Bearer ' + $($accessToken.access_token)
        'Content-Type' = "application/json"
    }
}
function Open-HRConnection(){
    param(
        [string] $username,
        [string] $password,
        [string] $domain,
        [string] $url
    )
    $Credentials = New-Object psobject -Property @{
        username = $username
        password = $password
        domain = $domain
    }
    return invoke-restmethod -Method Post -uri "$url/rest/login" -ContentType "application/json" -Body ($Credentials | ConvertTo-Json)
}
function Close-HRConnection(){
    param(
        $accessToken,
        $url
    )
    return Invoke-RestMethod -Method post -uri "$url/rest/logout" -ContentType "application/json" -Body ($accessToken | ConvertTo-Json)
}
try{
$accessToken = Open-HRConnection -username $username -password $UnsecurePassword -domain $Domain -url $url
Set-Clipboard (Get-HRHeader -accessToken $accessToken).Authorization
}
catch{
    write-host "Error while authenticating"
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s