The scripts shown here are provided as examples of how to integrate the Neo API.
These scripts are provided only as information and are not intended to be used in production environments.
Authentication#
This script will re-use an existing token or login for a new one, and return the account name and tenant name that is authenticated with that token.
param (
[Parameter(Mandatory = $true)][string]$user_id,
[Parameter(Mandatory = $true)][string]$api_key
)
.\Neo_reusable_script.ps1
if ($script:bearerToken) {
$response = (Invoke-SafeWebRequest -Method GET -Uri 'https://auth.getneo.com/api/v2/identity' -Headers @{ 'Authorization' = "Bearer $script:bearerToken" })
if ($response.StatusCode -ne 200) {
Write-Error "Failed to access Neo platform (identity = $($response.StatusCode))"
return
}
if (-not $response.Content) {
$script:bearerToken = $null
}
}
if (-not $script:bearerToken) {
$response = (Invoke-SafeWebRequest -Method GET -Uri "https://auth.getneo.com/api/v1/auth/login" -Headers @{ 'Authorization' = "Plaintext $user_id $api_key" })
if ($response.StatusCode -eq 418) {
Write-Error "Issue with login ($user_id): $((ConvertFrom-Json $response.Content).form.error)"
return
}
if ($response.StatusCode -ne 200) {
Write-Error "Failed to access Neo platform (auth/login = $($response.StatusCode))"
return
}
if (-not $response.Headers.Authorization.StartsWith('Bearer')) {
Write-Error "Service did not provide a bearer token"
return
}
$script:bearerToken = $response.Headers.Authorization.Substring(7)
Write-Host "Logged in: $user_id"
}
$response = (Invoke-SafeWebRequest -Method GET -Uri 'https://auth.getneo.com/api/v2/identity' -Headers @{ 'Authorization' = "Bearer ${script:bearerToken}" })
if ($response.StatusCode -ne 200) {
Write-Error "Token failed"
$script:bearerToken = $null
return
}
$id = (ConvertFrom-Json $response.Content)
Write-Host "Current user: $($id.name)"
Write-Host "Current tenant: $($id.currentClientName)"
Reusable scripts#
function Invoke-SafeWebRequest ([string]$Method, [string]$Uri, [PSObject]$Headers = $null, [PSObject]$Body = $null) {
try {
if ($Body) {
return (Invoke-WebRequest -Method $Method -Uri $uri -Headers $Headers -Body $Body)
} else {
return (Invoke-WebRequest -Method $Method -Uri $uri -Headers $Headers)
}
} catch {
return @{
"StatusCode" = $_.Exception.Response.StatusCode;
"StatusDescription" = $_.Exception.Response.StatusCode;
"Headers" = $_.Exception.Response.Headers;
"Content" = [IO.StreamReader]::new($_.Exception.Response.GetResponseStream()).ReadToEnd();
"RawContentLength" = $_.Exception.Response.ContentLength;
}
}
}