Describe the problem/error/question
I am trying to create the connection for Opera Cloud.
Documentation:
It has an OAuth Token request that returns:
{
"access_token": "xyz",
"expires_in": 3600,
"token_type": "Bearer"
}
The POST request needs:
- x-www-form-urlencoded body
- Basic Auth with CLIENT_ID & CLIENT_SECRET
- x-app-key in Headers
- username, password, grant_type: ‘password’ in body
What is the error message (if any)?
I noticed the httpRequest is not performed in preAuthentication. If I console log before & after that http call I can see the first log only.
I am trying to save the access_token in sessionToken, which has property expirable: true and use authenticate to pass the parameters needed in headers (x-app-key, x-hotelid, Authorization) for the authentication.
Similar to what was recommended in this thread:
The problem is that sessionToken remains empty and the httpRequest inside preAuthentication is not performing. The same request with same parameters works in Postman.
Please share your workflow
This is my credentials.ts file:
import {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestHelper,
INodeProperties,
} from 'n8n-workflow';
export class OperaCloudOAuth2Api implements ICredentialType {
name = 'operaCloudOAuth2Api';
displayName = 'Opera Cloud OAuth2 API';
properties: INodeProperties[] = [
{
displayName: 'Username',
name: 'username',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Password',
name: 'password',
type: 'string',
typeOptions: {
password: true,
},
default: '',
required: true,
},
{
displayName: 'Client ID',
name: 'clientId',
type: 'string',
default: '',
required: true,
},
{
displayName: 'Client Secret',
name: 'clientSecret',
type: 'string',
typeOptions: {
password: true,
},
default: '',
required: true,
},
{
displayName: 'Gateway URL',
name: 'gatewayUrl',
type: 'string',
default: '',
required: true,
},
{
displayName: 'App Key',
name: 'appKey',
type: 'string',
typeOptions: {
password: true,
},
default: '',
required: true
},
{
displayName: 'Session Token',
name: 'sessionToken',
type: 'hidden',
typeOptions: {
expirable: true,
},
default: '',
},
{
displayName: 'Hotel ID',
name: 'hotelId',
type: 'string',
required: true,
default: '',
},
];
async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) {
// make reques to get access token
const basicAuthKey = Buffer.from(
`${credentials.clientId}:${credentials.clientSecret}`,
).toString('base64');
const url = `${credentials.gatewayUrl}/oauth/v1/tokens`;
const { access_token } = (await this.helpers.httpRequest({
method: 'POST',
url: url,
headers: {
Authorization: `Basic ${basicAuthKey}`,
"x-app-key": credentials.appKey
},
body: {
username: credentials.username,
password: credentials.password,
grant_type: "password"
},
})) as { access_token: string };
return { sessionToken: access_token };
}
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
'x-app-key': '={{$credentials.appKey}}',
'x-hotelid': '={{$credentials.hotelId}}',
Authorization: '=Bearer {{$credentials.sessionToken}}'
},
},
};
test: ICredentialTestRequest = {
request: {
baseURL: '',
url: '',
}}
}
Can someone point what I’m doing wrong here or what might be the reason for this problem?
Let me know if you need more information and I’ll gladly provide.
Thank you!
Information on your n8n setup
- n8n version: 0.233.1
- Running n8n via: npm
- Operating system: MAC