Using PowerShell API Scripts

Here are some guidelines to check when utilizing a PowerShell script to make API calls.

1. Make sure $requestvars has the correct variables listed, and in the correct order.
For example, A GET script should look like:
 $requestVars = $httpVerb + $epoch + $resourcePath
A POST, PATCH, or PUT script should look like:
$requestVars = $httpVerb + $epoch + $data + $resourcePath

2. Be sure if the $accessId or $accessKey has a $in the string that it’s in single quotes, not double quotes. This would try to add a null variable inside the string and cause the authentication to fail.

3. Verify the responses are constructed correctly.
For a GET request,  $response should look like this:
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers

For a POST, PATCH, or PUT request
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers

4. Check that the $url variable lists the $resourcePathand the $queryParms if needed but never the $data.

$url = "https://$$resourcePath"
$url = "https://$$resourcePath$queryParams"

5. Before the $response, ensure the signature and the headers are built like so

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

If all of this looks good then check the $queryParms and $data and make sure the quotes match up and that anything that should be an integer is not in quotes.