OAuth2 support

This commit is contained in:
Rupenieks
2020-06-16 09:27:47 +02:00
parent acfc4fc39d
commit e5683a90ab
4 changed files with 105 additions and 16 deletions

View File

@@ -9,34 +9,39 @@ import {
import { IDataObject } from 'n8n-workflow';
export async function acuitySchedulingApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('acuitySchedulingApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
auth: {
user: credentials.userId as string,
password: credentials.apiKey as string,
},
auth: {},
method,
qs,
body,
uri: uri ||`https://acuityscheduling.com/api/v1${resource}`,
json: true
};
try {
return await this.helpers.request!(options);
} catch (error) {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('acuitySchedulingApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let errorMessage = error.message;
if (error.response.body && error.response.body.message) {
errorMessage = `[${error.response.body.status_code}] ${error.response.body.message}`;
options.auth = {
user: credentials.userId as string,
password: credentials.apiKey as string,
};
return await this.helpers.request!(options);
} else {
delete options.auth;
//@ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'acuitySchedulingOAuth2Api', options, true);
}
throw new Error('Acuity Scheduling Error: ' + errorMessage);
} catch (error) {
throw new Error('Acuity Scheduling Error: ' + error.message);
}
}