mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-12 15:44:06 -08:00
⚡ Google Drive OAuth2 support
This commit is contained in:
parent
afe0d16a9a
commit
d3829c90c2
|
@ -0,0 +1,26 @@
|
||||||
|
import {
|
||||||
|
ICredentialType,
|
||||||
|
NodePropertyTypes,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
const scopes = [
|
||||||
|
'https://www.googleapis.com/auth/drive',
|
||||||
|
'https://www.googleapis.com/auth/drive.appdata',
|
||||||
|
'https://www.googleapis.com/auth/drive.photos.readonly',
|
||||||
|
];
|
||||||
|
|
||||||
|
export class GoogleDriveOAuth2Api implements ICredentialType {
|
||||||
|
name = 'googleDriveOAuth2Api';
|
||||||
|
extends = [
|
||||||
|
'googleOAuth2Api',
|
||||||
|
];
|
||||||
|
displayName = 'Google Drive OAuth2 API';
|
||||||
|
properties = [
|
||||||
|
{
|
||||||
|
displayName: 'Scope',
|
||||||
|
name: 'scope',
|
||||||
|
type: 'hidden' as NodePropertyTypes,
|
||||||
|
default: scopes.join(' '),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
142
packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts
Normal file
142
packages/nodes-base/nodes/Google/Drive/GenericFunctions.ts
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
import {
|
||||||
|
OptionsWithUri,
|
||||||
|
} from 'request';
|
||||||
|
|
||||||
|
import {
|
||||||
|
IExecuteFunctions,
|
||||||
|
IExecuteSingleFunctions,
|
||||||
|
ILoadOptionsFunctions,
|
||||||
|
} from 'n8n-core';
|
||||||
|
|
||||||
|
import {
|
||||||
|
IDataObject,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
|
import * as moment from 'moment-timezone';
|
||||||
|
|
||||||
|
import * as jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
|
export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
|
||||||
|
|
||||||
|
let options: OptionsWithUri = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
method,
|
||||||
|
body,
|
||||||
|
qs,
|
||||||
|
uri: uri || `https://www.googleapis.com${resource}`,
|
||||||
|
json: true,
|
||||||
|
};
|
||||||
|
options = Object.assign({}, options, option);
|
||||||
|
try {
|
||||||
|
if (Object.keys(body).length === 0) {
|
||||||
|
delete options.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authenticationMethod === 'serviceAccount') {
|
||||||
|
const credentials = this.getCredentials('googleApi');
|
||||||
|
|
||||||
|
if (credentials === undefined) {
|
||||||
|
throw new Error('No credentials got returned!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
|
||||||
|
|
||||||
|
options.headers!.Authorization = `Bearer ${access_token}`;
|
||||||
|
//@ts-ignore
|
||||||
|
return await this.helpers.request(options);
|
||||||
|
} else {
|
||||||
|
//@ts-ignore
|
||||||
|
return await this.helpers.requestOAuth2.call(this, 'googleDriveOAuth2Api', options);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.body && error.response.body.error) {
|
||||||
|
|
||||||
|
let errorMessages;
|
||||||
|
|
||||||
|
if (error.response.body.error.errors) {
|
||||||
|
// Try to return the error prettier
|
||||||
|
errorMessages = error.response.body.error.errors;
|
||||||
|
|
||||||
|
errorMessages = errorMessages.map((errorItem: IDataObject) => errorItem.message);
|
||||||
|
|
||||||
|
errorMessages = errorMessages.join('|');
|
||||||
|
|
||||||
|
} else if (error.response.body.error.message){
|
||||||
|
errorMessages = error.response.body.error.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`Google Drive error response [${error.statusCode}]: ${errorMessages}`);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string ,method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
|
||||||
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
|
let responseData;
|
||||||
|
query.maxResults = 100;
|
||||||
|
|
||||||
|
do {
|
||||||
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
|
||||||
|
query.pageToken = responseData['nextPageToken'];
|
||||||
|
returnData.push.apply(returnData, responseData[propertyName]);
|
||||||
|
} while (
|
||||||
|
responseData['nextPageToken'] !== undefined &&
|
||||||
|
responseData['nextPageToken'] !== ''
|
||||||
|
);
|
||||||
|
|
||||||
|
return returnData;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, credentials: IDataObject) : Promise<IDataObject> {
|
||||||
|
//https://developers.google.com/identity/protocols/oauth2/service-account#httprest
|
||||||
|
|
||||||
|
const scopes = [
|
||||||
|
'https://www.googleapis.com/auth/drive',
|
||||||
|
'https://www.googleapis.com/auth/drive.appdata',
|
||||||
|
'https://www.googleapis.com/auth/drive.photos.readonly',
|
||||||
|
];
|
||||||
|
|
||||||
|
const now = moment().unix();
|
||||||
|
|
||||||
|
const signature = jwt.sign(
|
||||||
|
{
|
||||||
|
'iss': credentials.email as string,
|
||||||
|
'sub': credentials.email as string,
|
||||||
|
'scope': scopes.join(' '),
|
||||||
|
'aud': `https://oauth2.googleapis.com/token`,
|
||||||
|
'iat': now,
|
||||||
|
'exp': now + 3600,
|
||||||
|
},
|
||||||
|
credentials.privateKey as string,
|
||||||
|
{
|
||||||
|
algorithm: 'RS256',
|
||||||
|
header: {
|
||||||
|
'kid': credentials.privateKey as string,
|
||||||
|
'typ': 'JWT',
|
||||||
|
'alg': 'RS256',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const options: OptionsWithUri = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
method: 'POST',
|
||||||
|
form: {
|
||||||
|
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
||||||
|
assertion: signature,
|
||||||
|
},
|
||||||
|
uri: 'https://oauth2.googleapis.com/token',
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
|
||||||
|
//@ts-ignore
|
||||||
|
return this.helpers.request(options);
|
||||||
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
import { google } from 'googleapis';
|
|
||||||
const { Readable } = require('stream');
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BINARY_ENCODING,
|
BINARY_ENCODING,
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
@ -12,8 +10,9 @@ import {
|
||||||
INodeType,
|
INodeType,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import { getAuthenticationClient } from '../GoogleApi';
|
import {
|
||||||
|
googleApiRequest,
|
||||||
|
} from './GenericFunctions';
|
||||||
|
|
||||||
export class GoogleDrive implements INodeType {
|
export class GoogleDrive implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -34,9 +33,43 @@ export class GoogleDrive implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'googleApi',
|
name: 'googleApi',
|
||||||
required: true,
|
required: true,
|
||||||
}
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'serviceAccount',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'googleDriveOAuth2Api',
|
||||||
|
required: true,
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
authentication: [
|
||||||
|
'oAuth2',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
properties: [
|
||||||
|
{
|
||||||
|
displayName: 'Authentication',
|
||||||
|
name: 'authentication',
|
||||||
|
type: 'options',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'Service Account',
|
||||||
|
value: 'serviceAccount',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'OAuth2',
|
||||||
|
value: 'oAuth2',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
default: 'serviceAccount',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Resource',
|
displayName: 'Resource',
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
|
@ -813,26 +846,6 @@ export class GoogleDrive implements INodeType {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
const credentials = this.getCredentials('googleApi');
|
|
||||||
|
|
||||||
if (credentials === undefined) {
|
|
||||||
throw new Error('No credentials got returned!');
|
|
||||||
}
|
|
||||||
|
|
||||||
const scopes = [
|
|
||||||
'https://www.googleapis.com/auth/drive',
|
|
||||||
'https://www.googleapis.com/auth/drive.appdata',
|
|
||||||
'https://www.googleapis.com/auth/drive.photos.readonly',
|
|
||||||
];
|
|
||||||
|
|
||||||
const client = await getAuthenticationClient(credentials.email as string, credentials.privateKey as string, scopes);
|
|
||||||
|
|
||||||
const drive = google.drive({
|
|
||||||
version: 'v3',
|
|
||||||
// @ts-ignore
|
|
||||||
auth: client,
|
|
||||||
});
|
|
||||||
|
|
||||||
const resource = this.getNodeParameter('resource', 0) as string;
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
|
|
||||||
|
@ -857,22 +870,20 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||||
|
|
||||||
const copyOptions = {
|
const body: IDataObject = {
|
||||||
fileId,
|
|
||||||
fields: queryFields,
|
fields: queryFields,
|
||||||
requestBody: {} as IDataObject,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionProperties = ['name', 'parents'];
|
const optionProperties = ['name', 'parents'];
|
||||||
for (const propertyName of optionProperties) {
|
for (const propertyName of optionProperties) {
|
||||||
if (options[propertyName] !== undefined) {
|
if (options[propertyName] !== undefined) {
|
||||||
copyOptions.requestBody[propertyName] = options[propertyName];
|
body[propertyName] = options[propertyName];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await drive.files.copy(copyOptions);
|
const response = await googleApiRequest.call(this, 'POST', `/drive/v3/files/${fileId}/copy`, body);
|
||||||
|
|
||||||
returnData.push(response.data as IDataObject);
|
returnData.push(response as IDataObject);
|
||||||
|
|
||||||
} else if (operation === 'download') {
|
} else if (operation === 'download') {
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
@ -881,15 +892,13 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||||
|
|
||||||
const response = await drive.files.get(
|
const requestOptions = {
|
||||||
{
|
resolveWithFullResponse: true,
|
||||||
fileId,
|
encoding: null,
|
||||||
alt: 'media',
|
json: false,
|
||||||
},
|
};
|
||||||
{
|
|
||||||
responseType: 'arraybuffer',
|
const response = await googleApiRequest.call(this, 'GET', `/drive/v3/files/${fileId}`, {}, { alt: 'media' }, undefined, requestOptions);
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
let mimeType: string | undefined;
|
let mimeType: string | undefined;
|
||||||
if (response.headers['content-type']) {
|
if (response.headers['content-type']) {
|
||||||
|
@ -912,7 +921,7 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;
|
const dataPropertyNameDownload = this.getNodeParameter('binaryPropertyName', i) as string;
|
||||||
|
|
||||||
const data = Buffer.from(response.data as string);
|
const data = Buffer.from(response.body as string);
|
||||||
|
|
||||||
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data as unknown as Buffer, undefined, mimeType);
|
items[i].binary![dataPropertyNameDownload] = await this.helpers.prepareBinaryData(data as unknown as Buffer, undefined, mimeType);
|
||||||
|
|
||||||
|
@ -988,20 +997,19 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const pageSize = this.getNodeParameter('limit', i) as number;
|
const pageSize = this.getNodeParameter('limit', i) as number;
|
||||||
|
|
||||||
const res = await drive.files.list({
|
const qs = {
|
||||||
pageSize,
|
pageSize,
|
||||||
orderBy: 'modifiedTime',
|
orderBy: 'modifiedTime',
|
||||||
fields: `nextPageToken, files(${queryFields})`,
|
fields: `nextPageToken, files(${queryFields})`,
|
||||||
spaces: querySpaces,
|
spaces: querySpaces,
|
||||||
corpora: queryCorpora,
|
|
||||||
driveId,
|
|
||||||
q: queryString,
|
q: queryString,
|
||||||
includeItemsFromAllDrives: (queryCorpora !== '' || driveId !== ''), // Actually depracated,
|
includeItemsFromAllDrives: (queryCorpora !== '' || driveId !== ''),
|
||||||
supportsAllDrives: (queryCorpora !== '' || driveId !== ''), // see https://developers.google.com/drive/api/v3/reference/files/list
|
supportsAllDrives: (queryCorpora !== '' || driveId !== ''),
|
||||||
// However until June 2020 still needs to be set, to avoid API errors.
|
};
|
||||||
});
|
|
||||||
|
|
||||||
const files = res!.data.files;
|
const response = await googleApiRequest.call(this, 'GET', `/drive/v3/files`, {}, qs);
|
||||||
|
|
||||||
|
const files = response!.files;
|
||||||
|
|
||||||
return [this.helpers.returnJsonArray(files as IDataObject[])];
|
return [this.helpers.returnJsonArray(files as IDataObject[])];
|
||||||
|
|
||||||
|
@ -1044,29 +1052,35 @@ export class GoogleDrive implements INodeType {
|
||||||
const name = this.getNodeParameter('name', i) as string;
|
const name = this.getNodeParameter('name', i) as string;
|
||||||
const parents = this.getNodeParameter('parents', i) as string[];
|
const parents = this.getNodeParameter('parents', i) as string[];
|
||||||
|
|
||||||
const response = await drive.files.create({
|
let qs: IDataObject = {
|
||||||
requestBody: {
|
|
||||||
name,
|
|
||||||
originalFilename,
|
|
||||||
parents,
|
|
||||||
},
|
|
||||||
fields: queryFields,
|
fields: queryFields,
|
||||||
media: {
|
uploadType: 'media',
|
||||||
mimeType,
|
};
|
||||||
body: ((buffer: Buffer) => {
|
|
||||||
const readableInstanceStream = new Readable({
|
|
||||||
read() {
|
|
||||||
this.push(buffer);
|
|
||||||
this.push(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return readableInstanceStream;
|
const requestOptions = {
|
||||||
})(body),
|
headers: {
|
||||||
|
'Content-Type': mimeType,
|
||||||
|
'Content-Length': body.byteLength,
|
||||||
},
|
},
|
||||||
});
|
encoding: null,
|
||||||
|
json: false,
|
||||||
|
};
|
||||||
|
|
||||||
returnData.push(response.data as IDataObject);
|
let response = await googleApiRequest.call(this, 'POST', `/upload/drive/v3/files`, body, qs, undefined, requestOptions);
|
||||||
|
|
||||||
|
body = {
|
||||||
|
mimeType,
|
||||||
|
name,
|
||||||
|
originalFilename,
|
||||||
|
};
|
||||||
|
|
||||||
|
qs = {
|
||||||
|
addParents: parents.join(','),
|
||||||
|
};
|
||||||
|
|
||||||
|
response = await googleApiRequest.call(this, 'PATCH', `/drive/v3/files/${JSON.parse(response).id}`, body, qs);
|
||||||
|
|
||||||
|
returnData.push(response as IDataObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (resource === 'folder') {
|
} else if (resource === 'folder') {
|
||||||
|
@ -1077,19 +1091,19 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const name = this.getNodeParameter('name', i) as string;
|
const name = this.getNodeParameter('name', i) as string;
|
||||||
|
|
||||||
const fileMetadata = {
|
const body = {
|
||||||
name,
|
name,
|
||||||
mimeType: 'application/vnd.google-apps.folder',
|
mimeType: 'application/vnd.google-apps.folder',
|
||||||
parents: options.parents || [],
|
parents: options.parents || [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await drive.files.create({
|
const qs = {
|
||||||
// @ts-ignore
|
|
||||||
resource: fileMetadata,
|
|
||||||
fields: queryFields,
|
fields: queryFields,
|
||||||
});
|
};
|
||||||
|
|
||||||
returnData.push(response.data as IDataObject);
|
const response = await googleApiRequest.call(this, 'POST', '/drive/v3/files', body, qs);
|
||||||
|
|
||||||
|
returnData.push(response as IDataObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (['file', 'folder'].includes(resource)) {
|
if (['file', 'folder'].includes(resource)) {
|
||||||
|
@ -1100,9 +1114,7 @@ export class GoogleDrive implements INodeType {
|
||||||
|
|
||||||
const fileId = this.getNodeParameter('fileId', i) as string;
|
const fileId = this.getNodeParameter('fileId', i) as string;
|
||||||
|
|
||||||
await drive.files.delete({
|
const response = await googleApiRequest.call(this, 'DELETE', `/drive/v3/files/${fileId}`);
|
||||||
fileId,
|
|
||||||
});
|
|
||||||
|
|
||||||
// If we are still here it did succeed
|
// If we are still here it did succeed
|
||||||
returnData.push({
|
returnData.push({
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
|
|
||||||
import { JWT } from 'google-auth-library';
|
|
||||||
import { google } from 'googleapis';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the authentication client needed to access spreadsheet
|
|
||||||
*/
|
|
||||||
export async function getAuthenticationClient(email: string, privateKey: string, scopes: string[]): Promise <JWT> {
|
|
||||||
const client = new google.auth.JWT(
|
|
||||||
email,
|
|
||||||
undefined,
|
|
||||||
privateKey,
|
|
||||||
scopes,
|
|
||||||
undefined
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO: Check later if this or the above should be cached
|
|
||||||
await client.authorize();
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
return client;
|
|
||||||
}
|
|
|
@ -58,6 +58,7 @@
|
||||||
"dist/credentials/GitlabApi.credentials.js",
|
"dist/credentials/GitlabApi.credentials.js",
|
||||||
"dist/credentials/GoogleApi.credentials.js",
|
"dist/credentials/GoogleApi.credentials.js",
|
||||||
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
|
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
|
||||||
|
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GoogleOAuth2Api.credentials.js",
|
"dist/credentials/GoogleOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GoogleSheetsOAuth2Api.credentials.js",
|
"dist/credentials/GoogleSheetsOAuth2Api.credentials.js",
|
||||||
"dist/credentials/GumroadApi.credentials.js",
|
"dist/credentials/GumroadApi.credentials.js",
|
||||||
|
@ -319,7 +320,6 @@
|
||||||
"formidable": "^1.2.1",
|
"formidable": "^1.2.1",
|
||||||
"glob-promise": "^3.4.0",
|
"glob-promise": "^3.4.0",
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
"googleapis": "~50.0.0",
|
|
||||||
"imap-simple": "^4.3.0",
|
"imap-simple": "^4.3.0",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"lodash.get": "^4.4.2",
|
"lodash.get": "^4.4.2",
|
||||||
|
|
Loading…
Reference in a new issue