Merge pull request #633 from n8n-io/Hubspot-OAuth2-support

Hubspot OAuth2 support
This commit is contained in:
Ricardo Espinoza
2020-06-13 19:37:39 -04:00
committed by GitHub
5 changed files with 493 additions and 15 deletions

View File

@@ -14,12 +14,8 @@ import {
} from 'n8n-workflow';
export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { // tslint:disable-line:no-any
const authenticationMethod = this.getNodeParameter('authentication', 0);
const node = this.getNode();
const credentialName = Object.keys(node.credentials!)[0];
const credentials = this.getCredentials(credentialName);
query!.hapikey = credentials!.apiKey as string;
const options: OptionsWithUri = {
method,
qs: query,
@@ -28,8 +24,18 @@ export async function hubspotApiRequest(this: IHookFunctions | IExecuteFunctions
json: true,
useQuerystring: true,
};
try {
return await this.helpers.request!(options);
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('hubspotApi');
options.qs.hapikey = credentials!.apiKey as string;
return await this.helpers.request!(options);
} else {
// @ts-ignore
return await this.helpers.requestOAuth2!.call(this, 'hubspotOAuth2Api', options, 'Bearer');
}
} catch (error) {
if (error.response && error.response.body && error.response.body.errors) {
// Try to return the error prettier

View File

@@ -73,9 +73,44 @@ export class Hubspot implements INodeType {
{
name: 'hubspotApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'hubspotOAuth2Api',
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 method of authentication.',
},
{
displayName: 'Resource',
name: 'resource',

View File

@@ -37,6 +37,24 @@ export class HubspotTrigger implements INodeType {
{
name: 'hubspotDeveloperApi',
required: true,
displayOptions: {
show: {
authentication: [
'developerApi',
],
},
},
},
{
name: 'hubspotOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
@@ -54,6 +72,23 @@ export class HubspotTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Developer API',
value: 'developerApi',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'developerApi',
description: 'The method of authentication.',
},
{
displayName: 'App ID',
name: 'appId',
@@ -246,7 +281,21 @@ export class HubspotTrigger implements INodeType {
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const credentials = this.getCredentials('hubspotDeveloperApi');
const authenticationMethod = this.getNodeParameter('authentication') as string;
let credentials : IDataObject;
if (authenticationMethod === 'hubspotDeveloperApi') {
credentials = this.getCredentials('hubspotDeveloperApi') as IDataObject;
} else {
credentials = this.getCredentials('hubspotOAuth2Api') as IDataObject;
}
if (credentials === undefined) {
throw new Error('No credentials found!');
}
const req = this.getRequestObject();
const bodyData = req.body;
const headerData = this.getHeaderData();