Handle-FetchSites function you should replace the hard-coded Invoke-RestMethod block (lines ~458–472

This commit is contained in:
2025-06-25 00:34:44 -04:00
parent 51fd370f9d
commit 78c66457af

View File

@@ -441,36 +441,28 @@ function Handle-FetchSites {
# 2) Fetch your Datto API creds from the webhook # 2) Fetch your Datto API creds from the webhook
Write-LogHybrid "Calling webhook for Datto credentials…" "Info" "FetchSites" Write-LogHybrid "Calling webhook for Datto credentials…" "Info" "FetchSites"
<#
$resp = Get-DattoApiCredentials -Password $N8nPassword
if (-not $resp) {
Write-LogHelper "Failed to retrieve Datto API credentials from n8n" Error DattoAuth
throw
}
# now exactly the same values are populated:
$Global:ApiUrl = $resp.ApiUrl
$Global:ApiKey = $resp.ApiKey
$Global:ApiSecretKey = $resp.ApiSecretKey
Write-LogHelper "Fetched Datto API credentials from n8n" Success DattoAuth
#> #could replace the following 15 lines
try { try {
$hdr = @{ "SVSMSPKit" = $pw } $creds = Get-DattoApiCredentials -Password $pw
$resp = Invoke-RestMethod -Uri $Global:DattoWebhookUrl -Headers $hdr -Method GET if (-not $creds) {
Write-LogHybrid "Webhook returned no credentials" Error FetchSites
# store for later RMM calls
$Global:ApiUrl = $resp.ApiUrl
$Global:ApiKey = $resp.ApiKey
$Global:ApiSecretKey = $resp.ApiSecretKey
Write-LogHybrid "Fetched and stored API credentials." "Success" "FetchSites"
} catch {
Write-LogHybrid "Webhook call failed: $($_.Exception.Message)" "Error" "FetchSites" -LogToEvent
returnRespondEmpty $Context 403 returnRespondEmpty $Context 403
return return
} }
# reuse the same globals from the entrypoint
$Global:ApiUrl = $creds.ApiUrl
$Global:ApiKey = $creds.ApiKey
$Global:ApiSecretKey = $creds.ApiSecretKey
Write-LogHybrid "Fetched and stored API credentials." Success FetchSites
} catch {
Write-LogHybrid "Credential-fetch error: $($_.Exception.Message)" Error FetchSites -LogToEvent
returnRespondEmpty $Context 500
return
}
# 3) Exchange for a bearer token # 3) Exchange for a bearer token
Write-LogHybrid "Requesting OAuth token" "Info" "FetchSites" Write-LogHybrid "Requesting OAuth token" "Info" "FetchSites"
try { try {