{"record":{"id":"c965b3df9b2981f0","repo":"n8n-io/n8n","slug":"failed-to-retrieve-access-token","errorCode":null,"errorMessage":"Failed to retrieve access token","messagePattern":"Failed to retrieve access token","errorType":"exception","errorClass":"NodeOperationError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/llms/LmChatAzureOpenAi/credentials/N8nOAuth2TokenCredential.ts","lineNumber":23,"sourceCode":"import { NodeOperationError } from 'n8n-workflow';\n\nimport type { AzureEntraCognitiveServicesOAuth2ApiCredential } from '../types';\n/**\n * Adapts n8n's credential retrieval into the TokenCredential interface expected by @azure/identity\n */\nexport class N8nOAuth2TokenCredential implements TokenCredential {\n\tconstructor(\n\t\tprivate node: INode,\n\t\tprivate credential: AzureEntraCognitiveServicesOAuth2ApiCredential,\n\t) {}\n\n\t/**\n\t * Gets an access token from OAuth credential\n\t */\n\tasync getToken(): Promise<AccessToken | null> {\n\t\ttry {\n\t\t\tif (!this.credential?.oauthTokenData?.access_token) {\n\t\t\t\tthrow new NodeOperationError(this.node, 'Failed to retrieve access token');\n\t\t\t}\n\t\t\tconst oAuthClient = new ClientOAuth2({\n\t\t\t\tclientId: this.credential.clientId,\n\t\t\t\tclientSecret: this.credential.clientSecret,\n\t\t\t\taccessTokenUri: this.credential.accessTokenUrl,\n\t\t\t\tscopes: this.credential.scope?.split(' '),\n\t\t\t\tauthentication: this.credential.authentication,\n\t\t\t\tauthorizationUri: this.credential.authUrl,\n\t\t\t\tadditionalBodyProperties: {\n\t\t\t\t\tresource: 'https://cognitiveservices.azure.com/',\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tconst token = await oAuthClient.credentials.getToken();\n\t\t\tconst data = token.data as ClientOAuth2TokenData & {\n\t\t\t\texpires_on: number;\n\t\t\t};\n\t\t\treturn {","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/llms/LmChatAzureOpenAi/credentials/N8nOAuth2TokenCredential.ts#L5-L41","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Open the Azure Entra OAuth2 credential in n8n and click 'Connect' to complete the OAuth flow.","Verify the credential's clientId, clientSecret, accessTokenUrl, scope, authUrl are filled correctly before connecting.","If the issue persists, delete and recreate the credential.","Check that the n8n OAuth callback URL is registered in the Azure app registration."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if (!credential?.oauthTokenData?.access_token) {\n  throw new Error('Connect the Azure Entra OAuth2 credential in n8n to obtain an access token.');\n}","typeGuard":"const hasAccessToken = (c: any): boolean =>\n  !!c?.oauthTokenData?.access_token;","tryCatchPattern":"// Pre-validate before invoking getToken; surface a re-connect prompt.","preventionTips":["Complete the OAuth flow in the credential UI before using the node.","Reconnect when tokens are cleared or after migration.","Register the n8n callback URL in the Azure app registration."],"tags":["azure","oauth2","credentials","authentication","token"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}