mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
Merge pull request #650 from n8n-io/SurveyMonkey-OAuth2-support
SurveyMonkey OAuth2 support
This commit is contained in:
commit
3c817788e7
|
@ -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'
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -14,19 +14,13 @@ import {
|
||||||
} from 'n8n-workflow';
|
} 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
|
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 authenticationMethod = this.getNodeParameter('authentication', 0);
|
||||||
const credentials = this.getCredentials('surveyMonkeyApi');
|
|
||||||
|
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
const endpoint = 'https://api.surveymonkey.com/v3';
|
const endpoint = 'https://api.surveymonkey.com/v3';
|
||||||
|
|
||||||
let options: OptionsWithUri = {
|
let options: OptionsWithUri = {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `bearer ${credentials.accessToken}`,
|
|
||||||
},
|
},
|
||||||
method,
|
method,
|
||||||
body,
|
body,
|
||||||
|
@ -34,6 +28,7 @@ export async function surveyMonkeyApiRequest(this: IExecuteFunctions | IWebhookF
|
||||||
uri: uri || `${endpoint}${resource}`,
|
uri: uri || `${endpoint}${resource}`,
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!Object.keys(body).length) {
|
if (!Object.keys(body).length) {
|
||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
@ -41,8 +36,22 @@ export async function surveyMonkeyApiRequest(this: IExecuteFunctions | IWebhookF
|
||||||
delete options.qs;
|
delete options.qs;
|
||||||
}
|
}
|
||||||
options = Object.assign({}, options, option);
|
options = Object.assign({}, options, option);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
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);
|
return await this.helpers.request!(options);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return await this.helpers.requestOAuth2?.call(this, 'surveyMonkeyOAuth2Api', options);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error.response.body.error.message;
|
const errorMessage = error.response.body.error.message;
|
||||||
if (errorMessage !== undefined) {
|
if (errorMessage !== undefined) {
|
||||||
|
|
|
@ -49,6 +49,24 @@ export class SurveyMonkeyTrigger implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'surveyMonkeyApi',
|
name: 'surveyMonkeyApi',
|
||||||
required: true,
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'accessToken',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'surveyMonkeyOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
webhooks: [
|
webhooks: [
|
||||||
|
@ -66,6 +84,23 @@ export class SurveyMonkeyTrigger implements INodeType {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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',
|
displayName: 'Type',
|
||||||
name: 'objectType',
|
name: 'objectType',
|
||||||
|
@ -453,11 +488,18 @@ export class SurveyMonkeyTrigger implements INodeType {
|
||||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
||||||
const event = this.getNodeParameter('event') as string;
|
const event = this.getNodeParameter('event') as string;
|
||||||
const objectType = this.getNodeParameter('objectType') 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 headerData = this.getHeaderData() as IDataObject;
|
||||||
const req = this.getRequestObject();
|
const req = this.getRequestObject();
|
||||||
const webhookName = this.getWebhookName();
|
const webhookName = this.getWebhookName();
|
||||||
|
|
||||||
|
if (authenticationMethod === 'accessToken') {
|
||||||
|
credentials = this.getCredentials('surveyMonkeyApi') as IDataObject;
|
||||||
|
} else {
|
||||||
|
credentials = this.getCredentials('surveyMonkeyOAuth2Api') as IDataObject;
|
||||||
|
}
|
||||||
|
|
||||||
if (webhookName === 'setup') {
|
if (webhookName === 'setup') {
|
||||||
// It is a create webhook confirmation request
|
// It is a create webhook confirmation request
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -118,6 +118,7 @@
|
||||||
"dist/credentials/SalesmateApi.credentials.js",
|
"dist/credentials/SalesmateApi.credentials.js",
|
||||||
"dist/credentials/SegmentApi.credentials.js",
|
"dist/credentials/SegmentApi.credentials.js",
|
||||||
"dist/credentials/SurveyMonkeyApi.credentials.js",
|
"dist/credentials/SurveyMonkeyApi.credentials.js",
|
||||||
|
"dist/credentials/SurveyMonkeyOAuth2Api.credentials.js",
|
||||||
"dist/credentials/TelegramApi.credentials.js",
|
"dist/credentials/TelegramApi.credentials.js",
|
||||||
"dist/credentials/TodoistApi.credentials.js",
|
"dist/credentials/TodoistApi.credentials.js",
|
||||||
"dist/credentials/TrelloApi.credentials.js",
|
"dist/credentials/TrelloApi.credentials.js",
|
||||||
|
|
Loading…
Reference in a new issue