22 Jul 2025
08:45 PM
- last edited on
23 Jul 2025
09:20 AM
by
AgataWlodarczyk
I have written this AppFunction to toggle the monitoring state of a host, and I have given it all the required
scope, but I am still getting this error below. Any idea? I am planning to develop this further to be able to use in a workflow.
Any feedback is much appreciated.
------------------------
Given this permission:
-----------------------
Scopes: app-settings:objects:read, app-settings:objects:write, settings:objects:read, settings:objects:write, settings:schemas:read, app-engine:functions:run, app-engine:apps:run
The Function:
{"statusCode":500,"message":"HOST-F6DC785465402DD8 - Failed to update monitoring state.\nDebug info:\nError name: 403\nError message: OAuth token is missing required scope. Use one of: [settings:objects:write]\nStack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]\n at se.postSettingsObjects (file:///invoker.js:5:19821)\n at eventLoopTick (ext:core/01_core.js:177:7)\n at async Ds (file:///invoker.js:5:47784)"}
Solved! Go to Solution.
23 Jul 2025 12:35 PM
I also get the same error when running from a workflow
Message: HOST-F6DC785465402DD8 - Failed to update monitoring state.
Debug info:
Error name: 403
Error message: OAuth token is missing required scope. Use one of: [settings:objects:write]
Stack trace: 403: OAuth token is missing required scope. Use one of: [settings:objects:write]
at SettingsObjectsClient.postSettingsObjects (file:///opt/sdk_modules/@dynatrace-sdk/client-classic-environment-v2/esm/index.js:23556:19)
at eventLoopTick (ext:core/01_core.js:177:7)
at async default (file:///script.ts:8:22)
24 Jul 2025 11:21 AM
I gift this function to the community, as it is fully working; Enjoy!
import { execution } from '@dynatrace-sdk/automation-utils';
import { settingsObjectsClient } from '@dynatrace-sdk/client-classic-environment-v2'; // Make sure this import is present
export default async function () {
// We need to obtain the list of Hosts and iterate through it
// The list can be an API call or an array in this function.
const hostId = "HOST-F6DC785465402DD8"; // Replace with actual host ID
try {
const response = await settingsObjectsClient.postSettingsObjects({
body: [
{
schemaId: "builtin:host.monitoring",
scope: hostId,
value: {
enabled: true,
},
},
],
});
const ex = await execution();
const message = `${hostId} - Monitoring state updated successfully.`;
console.log('Message:', message);
console.log('Automated script execution on behalf of', ex.trigger);
return { triggeredBy: ex.trigger };
} catch (error: unknown) {
let errorDetails = "";
if (error instanceof Error) {
errorDetails = `Error name: ${error.name}\n` +
`Error message: ${error.message}\n` +
(error.stack ? `Stack trace: ${error.stack}` : "No stack trace available.");
} else {
errorDetails = `Unknown error object: ${JSON.stringify(error, null, 2)}`;
}
const ex = await execution();
const message = `${hostId} - Failed to update monitoring state.\nDebug info:\n${errorDetails}`;
console.log('Message:', message);
return { triggeredBy: ex.trigger };
}
}
24 Jul 2025 11:29 AM - edited 24 Jul 2025 11:31 AM
@MarwanC So, what was reason it was throwing the error before? Just interested to know and thanks for sharing the working code 👍
24 Jul 2025 12:27 PM
added settings:objects:write permission. Enjoy