n8n-ikaue-nodes
Version:
IKAUE nodes for N8N, such as a custom BigQuery node or a Google Search Console node.
109 lines • 4.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.googleApiRequest = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const moment_1 = __importDefault(require("moment"));
const jwt = __importStar(require("jsonwebtoken"));
async function googleApiRequest(method, resource, body, qs = {}, uri, headers = {}) {
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount');
const options = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: 'https://www.googleapis.com/webmasters/v3/sites/' + resource + '/searchAnalytics/query',
json: true,
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
if (authenticationMethod === 'serviceAccount') {
const credentials = await this.getCredentials('googleApi');
if (credentials === undefined) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const { access_token } = await getAccessToken.call(this, credentials);
options.headers.Authorization = `Bearer ${access_token}`;
return await this.helpers.request(options);
}
else {
return await this.helpers.requestOAuth2.call(this, 'googleSearchConsoleOAuth2Api', options);
}
}
catch (error) {
if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') {
error.statusCode = '401';
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
exports.googleApiRequest = googleApiRequest;
function getAccessToken(credentials) {
const privateKey = credentials.privateKey.replace(/\\n/g, '\n').trim();
const scopes = [
'https://www.googleapis.com/auth/webmasters',
'https://www.googleapis.com/auth/webmasters.readonly',
];
const now = (0, moment_1.default)().unix();
const signature = jwt.sign({
iss: credentials.email,
sub: credentials.delegatedEmail || credentials.email,
scope: scopes.join(' '),
aud: `https://oauth2.googleapis.com/token`,
iat: now,
exp: now + 3600,
}, privateKey, {
algorithm: 'RS256',
header: {
kid: privateKey,
typ: 'JWT',
alg: 'RS256',
},
});
const options = {
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,
};
return this.helpers.request(options);
}
//# sourceMappingURL=GenericFunctions.js.map