Describe the problem/error/question
I’m developing a node and I was just going through cleaning up all the code according to the linting warnings and now I’m running into some problems with 401 errors: unauthorised via OAuth2 credentials. I’ve tried reverting back to the old code that I wrote and that still doesn’t work, so I’m starting to think Microsoft has changed the authentication mechanism a little.
What is the error message (if any)?
Troubleshooting with webhooks I can see that the API only returns the correct bearer token if the following fields are sent in the body:
- client_id
- client_secret
- resource
- grant_type
For example, the body of the request should look like:
client_id=token&grant_type=client_credentials&client_secret=secrettoken&resource=https%3A%2F%2Fapi.securitycenter.microsoft.com
I’m able to successfully use an HTTP request node with all these fields filled out correctly and have it return a valid token. However, I’m unable to get the generic OAuth2 or my custom node to return a valid session token.
I think I’ve narrowed it down to the fact that the API endpoint expects the “resource” body parameter to be populated, however, I can’t add that as a parameter in my credentials and have it sent to the body of the request.
Here’s my existing code for my credentials. How can I configure the newly added “resource” property to be sent in the body of the request for getting the token?
import {
IAuthenticateGeneric,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class MicrosoftDefenderOAuth2Api implements ICredentialType {
name = 'microsoftDefenderOAuth2Api';
displayName = 'Microsoft Defender OAuth2 API';
extends = ['oAuth2Api'];
documentationUrl = 'https://docs.n8n.io/integrations/creating-nodes/build/declarative-style-node/';
properties: INodeProperties[] = [
{
displayName: 'Grant Type',
name: 'grantType',
type: 'hidden',
default: 'clientCredentials',
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'string',
default: '',
required: true,
},
{
displayName: 'resource',
name: 'resource',
type: 'string',
default: 'https://api.securitycenter.microsoft.com',
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden',
default: 'https://securitycenter.onmicrosoft.com/windowsatpservice/.default',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden',
default: 'body',
},
];
}
Information on your n8n setup
- n8n version: 0.236.2
- Running n8n via (Docker, npm, n8n cloud, desktop app): npm
- Operating system: Mac OS