Forum Discussion

Joe_Williams's avatar
Joe_Williams
Icon for Professor rankProfessor
7 years ago

Update hostGroupIds With Rest API

I have the below code which results in a 401. Any ideas? I am attempting to update the hostGroupIds.

 

$status = $null
$body = $null
$response = $null

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

<# account info #>
$accessId = '*****'
$accessKey = '*****'
$company = '*****'

<# request details #>
$httpVerb = 'PATCH'
$resourcePath = '/device/devices/1210'
$queryParams = '?patchFields=hostGroupIds'
$data = '{"hostGroupIds":"187,258"}'

<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams

<# Get current time in milliseconds #>
$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)

<# Concatenate Request Details #>
$requestVars = $httpVerb + $epoch + $data + $resourcePath

<# Construct Signature #>
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
$signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
$signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
$signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))

<# Construct Headers #>
$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$auth)
$headers.Add("Content-Type",'application/json')

<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 

<# Print status and body of response #>
$status = $response.status
$body = $response.data | ConvertTo-Json -Depth 5
$devices = $response.data.items
$status
$body

 

  • On 5/10/2018 at 5:44 PM, Joe Williams said:

     

    
    [...]
    $data = '{"hostGroupIds":"187,258"}'
    [...]
    <# Make Request #>
    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 
    [...]

     

    Hi, Joe. You are including a data payload in your authorization signature, but not including it in your REST call. So when the API is checking your REST call against the signature, it doesn't match and you'll get authentication-like errors. 

     Your $data is already a properly formatted JSON string, so you can just add the -Body argument, like this. 

    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers -Body $data
    

     

  • On 5/10/2018 at 5:44 PM, Joe Williams said:

     

    
    [...]
    $data = '{"hostGroupIds":"187,258"}'
    [...]
    <# Make Request #>
    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 
    [...]

     

    Hi, Joe. You are including a data payload in your authorization signature, but not including it in your REST call. So when the API is checking your REST call against the signature, it doesn't match and you'll get authentication-like errors. 

     Your $data is already a properly formatted JSON string, so you can just add the -Body argument, like this. 

    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers -Body $data