Hi all, I am trying to develop a declarative style for Microsoft Defender however, I am running into a few issues with the OAuth Authentication when trying to use the Oauth2 n8n credentials.
Describe the issue/error/question
Following the Microsoft guide here I can get the auth token using some PowerShell code like the following:
# This script acquires the App Context Token and stores it in the variable $token for later use in the script.
# Paste your Tenant ID, App ID, and App Secret (App key) into the indicated quotes below.
$tenantId = '' ### Paste your tenant ID here
$appId = '' ### Paste your Application ID here
$appSecret = '' ### Paste your Application key here
$resourceAppIdUri = 'https://api.securitycenter.microsoft.com'
$oAuthUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$authBody = [Ordered] @{
resource = "$resourceAppIdUri"
client_id = "$appId"
client_secret = "$appSecret"
grant_type = 'client_credentials'
}
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$token = $authResponse.access_token
$token
This is easily replicated using the n8n HTTPS request node with the following:
What is the error message (if any)?
When I try and replicate this using a credential node, I always get a token that returns “Unauthorized”
Currently, this is what my credentials look like. Any help would be greatly appreciated.
export class MicrosoftDefenderApi implements ICredentialType {
name = 'MicrosoftDefenderApi';
extends = ['oAuth2Api'];
icon = 'file:Microsoft.svg';
displayName = 'Microsoft Defender OAuth2 API';
documentationUrl = 'microsoft';
properties: INodeProperties[] = [
{
displayName: 'Grant Type',
name: 'grantType',
type: 'hidden',
default: 'clientCredentials',
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'string',
default: 'https://login.microsoftonline.com/$TenantId/oauth2/token',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden',
default: 'body',
},
{
displayName: 'Resource',
name: 'resource',
type: 'hidden',
default: 'https://api.securitycenter.microsoft.com',
},
];
}
Information on your n8n setup
- n8n version: 0.190.0
- Running n8n via [Docker, npm, n8n.cloud, desktop app]: NPM