0

First time trying to deal with APIs, not usually a programmer of any sort.

API Docs: https://www.pt-lifespan.com/api/uat/housing/v1.1/lh/docs/index.html

I was looking to leverage PowerShell to obtain API data and export it into a .csv file. I've managed to get an API into the correct format using example and tools that don't require authentication.

I'm struggling to authenticating my requests using the API docs (above). So I have some questions;

  • What sort of API authentication is this? Everything I find talks about oauth2 but it doesn't seem to follow the same pattern. I've assume it's HMAC.
  • Wrote the below script following the instructions. I've verified the conversion by using this tool: https://www.devglan.com/online-tools/hmac-sha256-online.

I'm still getting the 401 error. Can anyone help spot where's its going wrong?

#Variables
$HttpMethod = "get".ToLower()
#https://www.pt-lifespan.com/api/uat/housing/v1.1/lh/public/Properties
$AbsoluteUri = "/api/uat/housing/v1.1/lh/public/Properties".ToLower()
$Component = "component".ToLower()
$ContentLength = "0"
$timestamp = (Get-Date -format yyyyMMddTHHmmss).ToString()
$seperator = "|"

#api keys
$apikey = "ket"
$apisecretkey = "secret"

#GetRequestToSign     

$RequestToSign = $HttpMethod + $seperator + $AbsoluteUri + $seperator + $ContentLength
 

#GetSignKey   
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($apisecretkey)
$step1key = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($timestamp))
#$step1key = [Convert]::ToBase64String($step1key)
 
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($step1key)
$step2key = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($Component+$key))
#$step2key = [Convert]::ToBase64String($step2key)    

#Signature
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($step2key)
$Signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($RequestToSign))
$Signature = [System.BitConverter]::ToString($Signature).Replace('-','').ToLower()    

$Headers = @{
    Authorization = $Signature
}

Invoke-RestMethod -uri https://www.pt-lifespan.com/api/uat/housing/v1.1/lh/public/Properties -Method $Method -Headers $Headers
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
user2261755
  • 159
  • 1
  • 3
  • 17

0 Answers0