• 0
Joe Williams

Update hostGroupIds With Rest API

Question

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

 

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0
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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.