Private/Curanet/M365/Get-CuranetM365APIToken.ps1

function Get-CuranetM365APIToken() {
    param(
        [Parameter(Mandatory)]
        [string] $SubscriptionId
    )
    if(!$Global:CuranetResellerSession) {
        New-CuranetResellerSession -Account 3850
    }
    $ServiceLoginCredentials = Get-CuranetServiceLoginCredentials -SubscriptionId $SubscriptionId -Account 3850
    (Invoke-WebRequest -UseBasicParsing -Uri "https://office365.curanet.dk/api/Login/ServiceLoginReseller" `
            -Method "POST" `
            -WebSession $Global:CuranetResellerSession `
            -Headers @{
            "authority" = "office365.curanet.dk"
            "method"    = "POST"
            "path"      = "/api/Login/ServiceLoginReseller"
            "origin"    = "https://reseller.curanet.dk"
        } `
            -ContentType "application/x-www-form-urlencoded; charset=UTF-8" ` -Form $ServiceLoginCredentials) | Out-Null
    $M365Token = $Global:CuranetResellerSession.Cookies.GetCookies("https://office365.curanet.dk/api/Login/ServiceLoginReseller")[0]
    if ($M365Token) {
        return $M365Token.Value
    }
    else {
        throw "Failed to get Curanet M365 API token"
    }
}