1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-09-11 13:15:28 +03:00

Merge pull request #650 from n8n-io/SurveyMonkey-OAuth2-support

SurveyMonkey OAuth2 support
This commit is contained in:
Ricardo Espinoza 2020-06-14 16:29:21 -04:00 committed by GitHub
commit 3c817788e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 9 deletions

View File

@ -0,0 +1,47 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class SurveyMonkeyOAuth2Api implements ICredentialType {
name = 'surveyMonkeyOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'SurveyMonkey OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://api.surveymonkey.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://api.surveymonkey.com/oauth/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'string' as NodePropertyTypes,
default: 'surveys_read,surveys_write,responses_read,responses_write,webhooks_read,webhooks_write',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body'
},
];
}

View File

@ -14,19 +14,13 @@ import {
} from 'n8n-workflow';
export async function surveyMonkeyApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('surveyMonkeyApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
const endpoint = 'https://api.surveymonkey.com/v3';
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${credentials.accessToken}`,
},
method,
body,
@ -34,6 +28,7 @@ export async function surveyMonkeyApiRequest(this: IExecuteFunctions | IWebhookF
uri: uri || `${endpoint}${resource}`,
json: true
};
if (!Object.keys(body).length) {
delete options.body;
}
@ -41,8 +36,22 @@ export async function surveyMonkeyApiRequest(this: IExecuteFunctions | IWebhookF
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
if ( authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('surveyMonkeyApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
// @ts-ignore
options.headers['Authorization'] = `bearer ${credentials.accessToken}`;
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2?.call(this, 'surveyMonkeyOAuth2Api', options);
}
} catch (error) {
const errorMessage = error.response.body.error.message;
if (errorMessage !== undefined) {

View File

@ -49,6 +49,24 @@ export class SurveyMonkeyTrigger implements INodeType {
{
name: 'surveyMonkeyApi',
required: true,
displayOptions: {
show: {
authentication: [
'accessToken',
],
},
},
},
{
name: 'surveyMonkeyOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
@ -66,6 +84,23 @@ export class SurveyMonkeyTrigger 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: 'Type',
name: 'objectType',
@ -453,11 +488,18 @@ export class SurveyMonkeyTrigger implements INodeType {
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const event = this.getNodeParameter('event') as string;
const objectType = this.getNodeParameter('objectType') as string;
const credentials = this.getCredentials('surveyMonkeyApi') as IDataObject;
const authenticationMethod = this.getNodeParameter('authentication') as string;
let credentials : IDataObject;
const headerData = this.getHeaderData() as IDataObject;
const req = this.getRequestObject();
const webhookName = this.getWebhookName();
if (authenticationMethod === 'accessToken') {
credentials = this.getCredentials('surveyMonkeyApi') as IDataObject;
} else {
credentials = this.getCredentials('surveyMonkeyOAuth2Api') as IDataObject;
}
if (webhookName === 'setup') {
// It is a create webhook confirmation request
return {};

View File

@ -118,6 +118,7 @@
"dist/credentials/SalesmateApi.credentials.js",
"dist/credentials/SegmentApi.credentials.js",
"dist/credentials/SurveyMonkeyApi.credentials.js",
"dist/credentials/SurveyMonkeyOAuth2Api.credentials.js",
"dist/credentials/TelegramApi.credentials.js",
"dist/credentials/TodoistApi.credentials.js",
"dist/credentials/TrelloApi.credentials.js",