Private/Curanet/Get-CuraDNSAPIToken.ps1

Set-Variable -Name DNSApiToken -Scope Global -Value $null

Set-Variable -Name DNSApiTokenExpire -Scope Global -Value $null

function Get-CuraDNSAPIToken {
    <#
        .SYNOPSIS
        Fetches and updates the API access_token used for authentication against the DNS API
 
        .NOTES
        Should not be called manually
    #>


    try {
        $secret = Get-CuraDNSAPIKey

        $body = @{
            grant_type      = 'client_credentials'
            client_id       = '5e37d077-36ad-4532-928b-7a4eb015009b'
            client_secret   = $secret
            scope           = 'dns'
        }

        $contentType = 'application/x-www-form-urlencoded'

        $reqToken = Invoke-WebRequest -Method POST -Uri https://apiauth.dk.team.blue/auth/realms/Curanet/protocol/openid-connect/token -body $body -ContentType $contentType

        $tokenJSON = ConvertFrom-Json $reqToken.Content

        Set-Variable -Name DNSApiToken -Scope Global -Value $tokenJSON

        Set-Variable -Name DNSApiTokenExpire -Scope Global -Value (Get-Date).AddSeconds($global:DNSApiToken.expires_in)
    }

    catch {

        Write-Error "Error getting token - $($_)"

        return $false

    }
}