UNPKG

n8n-nodes-instagram-integrations

Version:

N8N nodes for Instagram API integration with OAuth2 authentication

137 lines 5.33 kB
"use strict"; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccessToken = getAccessToken; exports.getInstagramBusinessAccountId = getInstagramBusinessAccountId; exports.instagramApiRequest = instagramApiRequest; exports.instagramApiRequestAllItems = instagramApiRequestAllItems; exports.validateSignature = validateSignature; const n8n_workflow_1 = require("n8n-workflow"); const crypto = __importStar(require("crypto")); async function getAccessToken() { const credentials = (await this.getCredentials('instagramOAuth2Api')); const now = Math.floor(Date.now() / 1000); const longLivedToken = credentials.longLivedToken; const tokenExpiresAt = credentials.tokenExpiresAt || 0; if (longLivedToken && tokenExpiresAt > now) { return longLivedToken; } const oauthTokenData = credentials.oauthTokenData; const accessToken = oauthTokenData === null || oauthTokenData === void 0 ? void 0 : oauthTokenData.access_token; if (!accessToken) { throw new Error('No access token found. Please reconnect your Instagram account.'); } if (longLivedToken && tokenExpiresAt <= now) { throw new Error('Instagram access token has expired. Please reconnect your account by clicking "Connect" in the credential settings.'); } return accessToken; } async function getInstagramBusinessAccountId() { try { const accessToken = await getAccessToken.call(this); const options = { method: 'GET', url: 'https://graph.instagram.com/v23.0/me', qs: { access_token: accessToken, fields: 'id,name,account_type,media_count,user_id,username', }, json: true, }; const response = await this.helpers.request(options); if (response && response.id) { return response.id; } throw new Error('No Instagram Business Account found. Make sure your Facebook Page is connected to an Instagram Business Account.'); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { message: 'Failed to fetch Instagram Business Account ID, Error: ' + error.message, }); } } async function instagramApiRequest(method, endpoint, body = {}, qs = {}) { const accessToken = await getAccessToken.call(this); const options = { method, body, qs: { ...qs, access_token: accessToken, }, url: `https://graph.instagram.com/v23.0${endpoint}`, json: true, }; if (Object.keys(body).length === 0) { delete options.body; } try { return await this.helpers.request(options); } catch (error) { throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } async function instagramApiRequestAllItems(method, endpoint, body = {}, qs = {}) { var _a, _b; const returnData = []; let responseData; qs.limit = 100; do { responseData = await instagramApiRequest.call(this, method, endpoint, body, qs); if (responseData.data) { returnData.push(...responseData.data); } if ((_a = responseData.paging) === null || _a === void 0 ? void 0 : _a.next) { const url = new URL(responseData.paging.next); qs.after = url.searchParams.get('after'); } else { break; } } while ((_b = responseData.paging) === null || _b === void 0 ? void 0 : _b.next); return returnData; } function validateSignature(payload, signature, appSecret) { const hmac = crypto.createHmac('sha256', appSecret); const expectedSignature = 'sha256=' + hmac.update(payload).digest('hex'); try { return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expectedSignature)); } catch (error) { return false; } } //# sourceMappingURL=GenericFunctions.js.map