diff --git a/StackMonkey.ps1 b/StackMonkey.ps1 index e90979b..b88e104 100644 --- a/StackMonkey.ps1 +++ b/StackMonkey.ps1 @@ -441,36 +441,28 @@ function Handle-FetchSites { # 2) Fetch your Datto API creds from the webhook 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 { - $hdr = @{ "SVSMSPKit" = $pw } - $resp = Invoke-RestMethod -Uri $Global:DattoWebhookUrl -Headers $hdr -Method GET - - # 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 +try { + $creds = Get-DattoApiCredentials -Password $pw + if (-not $creds) { + Write-LogHybrid "Webhook returned no credentials" Error FetchSites returnRespondEmpty $Context 403 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 Write-LogHybrid "Requesting OAuth token" "Info" "FetchSites" try {