n8n-io/n8n · error · NodeOperationError

Failed to retrieve access token

Error message

Failed to retrieve access token

What it means

Thrown inside N8nOAuth2TokenCredential.getToken() when the credential's oauthTokenData.access_token is missing. The credential is an Azure Entra ID (Cognitive Services) OAuth2 credential; without a stored access token, the credential cannot produce a valid AccessToken and the Azure SDK call would fail opaquely, so the node pre-empts with a clear message.

Source

Thrown at packages/@n8n/nodes-langchain/nodes/llms/LmChatAzureOpenAi/credentials/N8nOAuth2TokenCredential.ts:23

import { NodeOperationError } from 'n8n-workflow';

import type { AzureEntraCognitiveServicesOAuth2ApiCredential } from '../types';
/**
 * Adapts n8n's credential retrieval into the TokenCredential interface expected by @azure/identity
 */
export class N8nOAuth2TokenCredential implements TokenCredential {
	constructor(
		private node: INode,
		private credential: AzureEntraCognitiveServicesOAuth2ApiCredential,
	) {}

	/**
	 * Gets an access token from OAuth credential
	 */
	async getToken(): Promise<AccessToken | null> {
		try {
			if (!this.credential?.oauthTokenData?.access_token) {
				throw new NodeOperationError(this.node, 'Failed to retrieve access token');
			}
			const oAuthClient = new ClientOAuth2({
				clientId: this.credential.clientId,
				clientSecret: this.credential.clientSecret,
				accessTokenUri: this.credential.accessTokenUrl,
				scopes: this.credential.scope?.split(' '),
				authentication: this.credential.authentication,
				authorizationUri: this.credential.authUrl,
				additionalBodyProperties: {
					resource: 'https://cognitiveservices.azure.com/',
				},
			});

			const token = await oAuthClient.credentials.getToken();
			const data = token.data as ClientOAuth2TokenData & {
				expires_on: number;
			};
			return {

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Open the Azure Entra OAuth2 credential in n8n and click 'Connect' to complete the OAuth flow.
  2. Verify the credential's clientId, clientSecret, accessTokenUrl, scope, authUrl are filled correctly before connecting.
  3. If the issue persists, delete and recreate the credential.
  4. Check that the n8n OAuth callback URL is registered in the Azure app registration.
Defensive patterns

Strategy: validation

Validate before calling

if (!credential?.oauthTokenData?.access_token) {
  throw new Error('Connect the Azure Entra OAuth2 credential in n8n to obtain an access token.');
}

Type guard

const hasAccessToken = (c: any): boolean =>
  !!c?.oauthTokenData?.access_token;

Try / catch

// Pre-validate before invoking getToken; surface a re-connect prompt.

Prevention

When it happens

Trigger: OAuth2 credential was never connected through the n8n OAuth flow, the stored token was wiped/expired-and-not-refreshed, or oauthTokenData exists without an access_token field (e.g. only a refresh token).

Common situations: First use of a newly created Entra OAuth2 credential before the user clicks 'Connect'; token cleared by a migration; credential copied between environments without re-connecting; refresh flow failing silently leaving an incomplete token object.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/c965b3df9b2981f0. Report an issue: GitHub.