mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 04:04:06 -08:00
OAuth2 support, changed url to subdomain in credentials
This commit is contained in:
parent
516a56ea32
commit
bdad41f4e8
|
@ -8,10 +8,11 @@ export class ZendeskApi implements ICredentialType {
|
|||
displayName = 'Zendesk API';
|
||||
properties = [
|
||||
{
|
||||
displayName: 'URL',
|
||||
name: 'url',
|
||||
displayName: 'Subdomain',
|
||||
name: 'subdomain',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
description: 'The subdomain of your Zendesk work environment.',
|
||||
default: 'n8n',
|
||||
},
|
||||
{
|
||||
displayName: 'Email',
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
import {
|
||||
ICredentialType,
|
||||
NodePropertyTypes,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
||||
export class ZendeskOAuth2Api implements ICredentialType {
|
||||
name = 'zendeskOAuth2Api';
|
||||
extends = [
|
||||
'oAuth2Api',
|
||||
];
|
||||
displayName = 'Zendesk OAuth2 API';
|
||||
properties = [
|
||||
{
|
||||
displayName: 'Subdomain',
|
||||
name: 'subdomain',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'n8n',
|
||||
description: 'The subdomain of your Zendesk work environment.',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Authorization URL',
|
||||
name: 'authUrl',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'https://{SUBDOMAIN_HERE}.zendesk.com/oauth/authorizations/new',
|
||||
description: 'URL to get authorization code. Replace {SUBDOMAIN_HERE} with your subdomain.',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Access Token URL',
|
||||
name: 'accessTokenUrl',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'https://{SUBDOMAIN_HERE}.zendesk.com/oauth/tokens',
|
||||
description: 'URL to get access token. Replace {SUBDOMAIN_HERE} with your subdomain.',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Client ID',
|
||||
name: 'clientId',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Client Secret',
|
||||
name: 'clientSecret',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Scope',
|
||||
name: 'scope',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'write read',
|
||||
},
|
||||
{
|
||||
displayName: 'Auth URI Query Parameters',
|
||||
name: 'authQueryParameters',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: '',
|
||||
description: 'For some services additional query parameters have to be set which can be defined here.',
|
||||
placeholder: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: 'body',
|
||||
description: 'Resource to consume.',
|
||||
},
|
||||
];
|
||||
}
|
|
@ -14,26 +14,48 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
|
||||
export async function zendeskApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = this.getCredentials('zendeskApi');
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64');
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||
|
||||
let options: OptionsWithUri = {
|
||||
headers: { 'Authorization': `Basic ${base64Key}`},
|
||||
headers: {},
|
||||
method,
|
||||
qs,
|
||||
body,
|
||||
uri: uri ||`${credentials.url}/api/v2${resource}.json`,
|
||||
//@ts-ignore
|
||||
uri,
|
||||
json: true
|
||||
};
|
||||
|
||||
options = Object.assign({}, options, option);
|
||||
if (Object.keys(options.body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
} catch (err) {
|
||||
if (authenticationMethod === 'accessToken') {
|
||||
const credentials = this.getCredentials('zendeskApi');
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
const base64Key = Buffer.from(`${credentials.email}/token:${credentials.apiToken}`).toString('base64');
|
||||
options.uri = `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`;
|
||||
options.headers!['Authorization'] = `Basic ${base64Key}`;
|
||||
|
||||
return await this.helpers.request!(options);
|
||||
} else {
|
||||
const credentials = this.getCredentials('zendeskOAuth2Api');
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
options.uri = `https://${credentials.subdomain}.zendesk.com/api/v2${resource}.json`;
|
||||
|
||||
return await this.helpers.requestOAuth2!.call(this, 'zendeskOAuth2Api', options);
|
||||
}
|
||||
} catch(err) {
|
||||
let errorMessage = err.message;
|
||||
if (err.response && err.response.body && err.response.body.error) {
|
||||
errorMessage = err.response.body.error;
|
||||
|
|
|
@ -52,9 +52,44 @@ export class Zendesk implements INodeType {
|
|||
{
|
||||
name: 'zendeskApi',
|
||||
required: true,
|
||||
}
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'accessToken',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'zendeskOAuth2Api',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'oAuth2',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Access Token',
|
||||
value: 'accessToken',
|
||||
},
|
||||
{
|
||||
name: 'OAuth2',
|
||||
value: 'oAuth2',
|
||||
},
|
||||
],
|
||||
default: 'accessToken',
|
||||
description: 'The resource to operate on.',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
|
|
|
@ -126,6 +126,7 @@
|
|||
"dist/credentials/WooCommerceApi.credentials.js",
|
||||
"dist/credentials/WordpressApi.credentials.js",
|
||||
"dist/credentials/ZendeskApi.credentials.js",
|
||||
"dist/credentials/ZendeskOAuth2Api.credentials.js",
|
||||
"dist/credentials/ZohoOAuth2Api.credentials.js",
|
||||
"dist/credentials/ZulipApi.credentials.js"
|
||||
],
|
||||
|
|
Loading…
Reference in a new issue