Forum Discussion

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

HTTP Authentication Required?

I have two sets of code. 1 pulls all 'Services' or Website groups. The other is supposed to just pull the group name and id. The first works, the second doesn't. I am using the base PowerShell script laid out in the documentation.

If I remove the ?fields=name,id it works without issue. If I add it back it gives me this error "HTTP Status 401 - Unauthorizedtype Status
reportmessage Unauthorizeddescription This request requires HTTP authentication.LogicMonitor Technical Operations". Any help would be awesome.

<# request details #>
$httpVerb = 'GET'
$resourcePath = '/service/groups?fields=name,id'

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

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

 

  • The documentation isn't correct it appears in some areas.

    $httpVerb = 'GET'
    $resourcePath = '/setting/collectors'
    $queryParams = '?fields=id,description,hostname'
    
    $data = ''
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams
    
    <# Concatenate Request Details #>
    $requestVars = $httpVerb + $epoch + $data + $resourcePath

    I had to add a blank data variable. Once I did that everything worked. I found that string via the Python code.

  • @Joe Williams you shouldn't need the blank $data variable. the $requestVars should just have the $data removed when you're doing a GET. There's two different script samples, one is for a GET and one is for a POST. 

    The issue it looks like what you have above is your $queryParams, this needs to be the way it is in your second example. The auth fails because you're including them in the resourcePath information and that's breaking the authentication hash. In the code you posted second, it should look like this. Hope this helps! 

    <# request details #>
    $httpVerb = 'GET'
    $resourcePath = '/setting/collectors'
    $queryParams = '?fields=id,description,hostname'
      
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryVars
    
    <# 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 + $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

     

  • The documentation isn't correct it appears in some areas.

    $httpVerb = 'GET'
    $resourcePath = '/setting/collectors'
    $queryParams = '?fields=id,description,hostname'
    
    $data = ''
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams
    
    <# Concatenate Request Details #>
    $requestVars = $httpVerb + $epoch + $data + $resourcePath

    I had to add a blank data variable. Once I did that everything worked. I found that string via the Python code.