mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 06:34:05 -08:00
Merge pull request #667 from n8n-io/Mailchimp-OAuth2-Support
Mailchimp OAuth2 support
This commit is contained in:
commit
7f76ee9bcd
|
@ -0,0 +1,68 @@
|
|||
import {
|
||||
ICredentialType,
|
||||
NodePropertyTypes,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
||||
export class MailchimpOAuth2Api implements ICredentialType {
|
||||
name = 'mailchimpOAuth2Api';
|
||||
extends = [
|
||||
'oAuth2Api',
|
||||
];
|
||||
displayName = 'Mailchimp OAuth2 API';
|
||||
properties = [
|
||||
{
|
||||
displayName: 'Mailchimp Server',
|
||||
name: 'server',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'https://login.mailchimp.com/',
|
||||
description: 'The server to connect to.',
|
||||
},
|
||||
{
|
||||
displayName: 'Datacenter',
|
||||
name: 'dataCenter',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: 'us10',
|
||||
description: 'Datacenter that your Mailchimp application is hosted on. Found in the URL of your Mailchimp dashboard.',
|
||||
},
|
||||
{
|
||||
displayName: 'Authorization URL',
|
||||
name: 'authUrl',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: 'https://login.mailchimp.com/oauth2/authorize',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Access Token URL',
|
||||
name: 'accessTokenUrl',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: 'https://login.mailchimp.com/oauth2/token',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Metadata',
|
||||
name: 'metadataUrl',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: 'https://login.mailchimp.com/oauth2/metadata',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
displayName: 'Scope',
|
||||
name: 'scope',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Auth URI Query Parameters',
|
||||
name: 'authQueryParameters',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'hidden' as NodePropertyTypes,
|
||||
default: 'header',
|
||||
},
|
||||
];
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import {
|
||||
OptionsWithUri,
|
||||
OptionsWithUrl,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
|
@ -14,35 +14,51 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
|
||||
export async function mailchimpApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, qs: IDataObject = {} ,headers?: object): Promise<any> { // tslint:disable-line:no-any
|
||||
const credentials = this.getCredentials('mailchimpApi');
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
const headerWithAuthentication = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
||||
|
||||
if (!(credentials.apiKey as string).includes('-')) {
|
||||
throw new Error('The API key is not valid!');
|
||||
}
|
||||
|
||||
const datacenter = (credentials.apiKey as string).split('-').pop();
|
||||
const authenticationMethod = this.getNodeParameter('authentication', 0) as string;
|
||||
|
||||
const host = 'api.mailchimp.com/3.0';
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
headers: headerWithAuthentication,
|
||||
const options: OptionsWithUrl = {
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
method,
|
||||
qs,
|
||||
uri: `https://${datacenter}.${host}${endpoint}`,
|
||||
body,
|
||||
url: ``,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(body).length !== 0) {
|
||||
options.body = body;
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.helpers.request!(options);
|
||||
if (authenticationMethod === 'accessToken') {
|
||||
const credentials = this.getCredentials('mailchimpApi');
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new Error('No credentials got returned!');
|
||||
}
|
||||
|
||||
options.headers = Object.assign({}, headers, { Authorization: `apikey ${credentials.apiKey}` });
|
||||
|
||||
if (!(credentials.apiKey as string).includes('-')) {
|
||||
throw new Error('The API key is not valid!');
|
||||
}
|
||||
|
||||
const datacenter = (credentials.apiKey as string).split('-').pop();
|
||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
||||
|
||||
return await this.helpers.request!(options);
|
||||
} else {
|
||||
const credentials = this.getCredentials('mailchimpOAuth2Api');
|
||||
const datacenter = credentials!.dataCenter;
|
||||
|
||||
options.url = `https://${datacenter}.${host}${endpoint}`;
|
||||
//@ts-ignore
|
||||
return await this.helpers.requestOAuth2!.call(this, 'mailchimpOAuth2Api', options, 'bearer');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response.body && error.response.body.detail) {
|
||||
throw new Error(`Mailchimp Error response [${error.statusCode}]: ${error.response.body.detail}`);
|
||||
|
|
|
@ -69,9 +69,44 @@ export class Mailchimp implements INodeType {
|
|||
{
|
||||
name: 'mailchimpApi',
|
||||
required: true,
|
||||
}
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'accessToken',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'mailchimpOAuth2Api',
|
||||
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: 'Method of authentication.',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
|
@ -1536,6 +1571,7 @@ export class Mailchimp implements INodeType {
|
|||
responseData = { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(responseData)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
} else {
|
||||
|
|
|
@ -33,7 +33,25 @@ export class MailchimpTrigger implements INodeType {
|
|||
{
|
||||
name: 'mailchimpApi',
|
||||
required: true,
|
||||
}
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'accessToken',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'mailchimpOAuth2Api',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
authentication: [
|
||||
'oAuth2',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
webhooks: [
|
||||
{
|
||||
|
@ -50,6 +68,23 @@ export class MailchimpTrigger implements INodeType {
|
|||
}
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Authentication',
|
||||
name: 'authentication',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Access Token',
|
||||
value: 'accessToken',
|
||||
},
|
||||
{
|
||||
name: 'OAuth2',
|
||||
value: 'oAuth2',
|
||||
},
|
||||
],
|
||||
default: 'accessToken',
|
||||
description: 'Method of authentication.',
|
||||
},
|
||||
{
|
||||
displayName: 'List',
|
||||
name: 'list',
|
||||
|
|
|
@ -80,6 +80,7 @@
|
|||
"dist/credentials/KeapOAuth2Api.credentials.js",
|
||||
"dist/credentials/LinkFishApi.credentials.js",
|
||||
"dist/credentials/MailchimpApi.credentials.js",
|
||||
"dist/credentials/MailchimpOAuth2Api.credentials.js",
|
||||
"dist/credentials/MailgunApi.credentials.js",
|
||||
"dist/credentials/MailjetEmailApi.credentials.js",
|
||||
"dist/credentials/MailjetSmsApi.credentials.js",
|
||||
|
|
Loading…
Reference in a new issue