add OAuth2

This commit is contained in:
shraddha shaligram
2020-06-18 12:29:16 -07:00
12 changed files with 854 additions and 706 deletions

View File

@@ -9,21 +9,11 @@ import {
import { IDataObject } from 'n8n-workflow';
import * as _ from 'lodash';
export async function zoomApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: object = {},
query: object = {},
headers: {} | undefined = undefined,
option: {} = {}
): Promise<any> {
export async function zoomApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: object = {}, query: object = {}, headers: {} | undefined = undefined, option: {} = {}): Promise<any> { // tslint:disable-line:no-any
// tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter(
'authentication',
0,
'accessToken'
) as string;
const authenticationMethod = this.getNodeParameter('authentication', 0, 'accessToken') as string;
let options: OptionsWithUri = {
method,
headers: headers || {
@@ -31,7 +21,7 @@ export async function zoomApiRequest(
},
body,
qs: query,
uri: `https://zoom.us/oauth${resource}`,
uri: `https://api.zoom.us/v2${resource}`,
json: true
};
options = Object.assign({}, options, option);
@@ -41,6 +31,8 @@ export async function zoomApiRequest(
if (Object.keys(query).length === 0) {
delete options.qs;
}
console.log("options");
console.log(options);
try {
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('zoomApi');
@@ -48,15 +40,24 @@ export async function zoomApiRequest(
throw new Error('No credentials got returned!');
}
options.headers!.Authorization = `Bearer ${credentials.accessToken}`;
console.log("options if");
console.log(options);
//@ts-ignore
return await this.helpers.request(options);
} else {
console.log("options else");
console.log(options);
let credentials = this.getCredentials('zoomOAuth2Api');
// let oauthtoken1 = credentials!.oauthTokenData;
console.log(credentials);
console.log("credss");
//@ts-ignore
return await this.helpers.requestOAuth2.call(
this,
'zoomOAuth2Api',
options
);
return await this.helpers.requestOAuth2.call(this, 'zoomOAuth2Api', options);
}
} catch (error) {
if (error.statusCode === 401) {
@@ -66,16 +67,17 @@ export async function zoomApiRequest(
if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier
throw new Error(
`Zoom error response [${error.statusCode}]: ${error.response.body.message}`
);
throw new Error(`Zoom error response [${error.statusCode}]: ${error.response.body.message}`);
}
// If that data does not exist for some reason return the actual error
throw error;
}
}
export async function zoomApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,

View File

@@ -3,13 +3,18 @@ import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription
INodeTypeDescription,
} from 'n8n-workflow';
import {
zoomApiRequest,
zoomApiRequestAllItems,
validateJSON
validateJSON,
} from './GenericFunctions';
import {
meetingOperations,
meetingFields,
} from './ZoomOperations';
export class Zoom implements INodeType {
description: INodeTypeDescription = {
displayName: 'Zoom',
@@ -25,25 +30,49 @@ export class Zoom implements INodeType {
icon: 'file:zoom.png',
inputs: ['main'],
outputs: ['main'],
// credentials: [
// {
// name: 'zoomApi',
// required: true,
// displayOptions: {
// show: {
// authentication: ['accessToken']
// }
// }
// },
// {
// name: 'zoomOAuth2Api',
// required: true,
// displayOptions: {
// show: {
// authentication: ['oAuth2']
// }
// }
// }
// ],
credentials: [
{
name: 'zoomApi',
required: true,
displayOptions: {
show: {
authentication: ['accessToken']
}
}
authentication: [
'accessToken',
],
},
},
},
{
name: 'zoomOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: ['oAuth2']
}
}
}
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
@@ -53,15 +82,15 @@ export class Zoom implements INodeType {
options: [
{
name: 'Access Token',
value: 'accessToken'
value: 'accessToken',
},
{
name: 'OAuth2',
value: 'oAuth2'
}
value: 'oAuth2',
},
],
default: 'accessToken',
description: 'The resource to operate on.'
description: 'The resource to operate on.',
},
{
displayName: 'Resource',
@@ -69,14 +98,17 @@ export class Zoom implements INodeType {
type: 'options',
options: [
{
name: 'meeting',
name: 'Meeting',
value: 'meeting'
}
],
default: 'meeting',
description: 'The resource to operate on.'
}
},
...meetingOperations,
...meetingFields
]
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
@@ -85,29 +117,26 @@ export class Zoom implements INodeType {
const length = (items.length as unknown) as number;
let qs: IDataObject;
let responseData;
const authentication = this.getNodeParameter('authentication', 0) as string;
const resource = this.getNodeParameter('resource', 0) as string;
// const operation = this.getNodeParameter('operation', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
console.log(this.getCredentials('zoomOAuth2Api'));
// for (let i = 0; i < length; i++) {
// qs = {};
// if (resource === 'channel') {
// //https://api.slack.com/methods/conversations.archive
// if (operation === 'archive') {
// const channel = this.getNodeParameter('channelId', i) as string;
// const body: IDataObject = {
// channel
// };
// responseData = await zoomApiRequest.call(
// this,
// 'POST',
// '/conversations.archive',
// body,
// qs
// );
// }
// }
// }
for (let i = 0; i < length; i++) {
qs = {};
if (resource === 'meeting') {
if (operation === 'get') {
const userId = this.getNodeParameter('userId', i) as string;
responseData = await zoomApiRequest.call(
this,
'GET',
`/meetings/${userId}`,
{},
qs
);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {

View File

@@ -0,0 +1,71 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const meetingOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'meeting',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a meeting',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a meeting',
},
{
name: 'Get',
value: 'get',
description: 'Retrieve a meeting',
},
{
name: 'Get All',
value: 'getAll',
description: 'Retrieve all meetings',
},
{
name: 'Update',
value: 'update',
description: 'Update a meeting',
}
],
default: 'create',
description: 'The operation to perform.',
}
] as INodeProperties[];
export const meetingFields = [
/* -------------------------------------------------------------------------- */
/* meeting:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'User Id',
name: 'userId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
operation: [
'get',
],
resource: [
'meeting',
],
},
},
description: 'User ID.',
},
] as INodeProperties[];