Private/Get-BearerToken.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function Get-BearerToken {
    [CmdletBinding()]
    param (
        $apikey
    )

    begin {
        Write-Verbose "Get-BearerToken: begin"
        $header = @{"Accept" = "application/json"}
        $body = (@{"token" = $apikey} | ConvertTo-Json)
       }

    process {
        try {
            $result = Invoke-RestMethod -Uri "$($script:siteinfo.url)/login/token" -Method Post -ContentType "application/json" -Headers $header -Body $body
            if ($result.token ) {
                Write-Verbose "Get-BearerToken: got token"
                ConvertTo-SecureString -AsPlainText -String $result.token
            }
        }
        catch {
           throw "Get-BearerToken: Cannot get token from api."
        }
    }

    end {

    }
}