UNPKG

n8n-nodes-instagram-integrations

Version:

N8N nodes for Instagram API integration with OAuth2 authentication

173 lines 6.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstagramOAuth2Api = void 0; class InstagramOAuth2Api { constructor() { this.name = 'instagramOAuth2Api'; this.extends = ['oAuth2Api']; this.displayName = 'Instagram OAuth2 API'; this.documentationUrl = 'https://developers.facebook.com/docs/instagram-api/getting-started'; this.properties = [ { displayName: 'Grant Type', name: 'grantType', type: 'hidden', default: 'authorizationCode', }, { displayName: 'Authorization URL', name: 'authUrl', type: 'hidden', default: 'https://api.instagram.com/oauth/authorize', }, { displayName: 'Access Token URL', name: 'accessTokenUrl', type: 'hidden', default: 'https://api.instagram.com/oauth/access_token', }, { displayName: 'Scope', name: 'scope', type: 'hidden', default: 'instagram_business_basic,instagram_business_manage_messages,instagram_business_manage_comments,instagram_business_content_publish', }, { displayName: 'Auth URI Query Parameters', name: 'authQueryParameters', type: 'hidden', default: '', }, { displayName: 'Authentication', name: 'authentication', type: 'hidden', default: 'body', }, { displayName: 'Account Information', name: 'accountInfoNotice', type: 'notice', default: '', description: 'After connecting your account, your Instagram Business Account details (username, ID, profile) will be automatically available. You can access this information in your workflow nodes. The system will automatically exchange your OAuth token for a long-lived token (60 days) and persist it, ensuring it survives n8n restarts.', }, { displayName: 'Client ID', name: 'clientId', type: 'string', default: '', required: true, description: 'The Instagram App ID from your Meta Developer Console. <a href="https://developers.facebook.com/apps/" target="_blank">Get it here</a>.', placeholder: '1234567890123456', }, { displayName: 'Client Secret', name: 'clientSecret', type: 'string', typeOptions: { password: true, }, default: '', required: true, description: 'The Instagram App Secret from your Meta Developer Console', placeholder: 'abc123def456...', }, { displayName: 'Webhook Verify Token', name: 'webhookVerifyToken', type: 'string', typeOptions: { password: true }, default: '', required: false, description: 'Optional: Custom verification token for webhook setup (minimum 20 characters). Only needed if using Instagram Trigger node.', placeholder: 'my_custom_verify_token_2024', }, { displayName: 'Long-Lived Token', name: 'longLivedToken', type: 'hidden', typeOptions: { expirable: true, }, default: '', }, { displayName: 'Token Expires At', name: 'tokenExpiresAt', type: 'hidden', default: 0, }, ]; this.test = { request: { baseURL: 'https://graph.instagram.com/v23.0', url: '/me', method: 'GET', qs: { fields: 'id,username', }, }, }; } async preAuthentication(credentials) { const now = Math.floor(Date.now() / 1000); const longLivedToken = credentials.longLivedToken; const tokenExpiresAt = credentials.tokenExpiresAt || 0; const clientSecret = credentials.clientSecret; const oauthTokenData = credentials.oauthTokenData; const shortLivedToken = oauthTokenData === null || oauthTokenData === void 0 ? void 0 : oauthTokenData.access_token; if (longLivedToken && tokenExpiresAt > 0) { const sevenDaysInSeconds = 7 * 24 * 60 * 60; if (tokenExpiresAt > now + sevenDaysInSeconds) { return {}; } const totalLifetime = 60 * 24 * 60 * 60; const tokenAge = now - (tokenExpiresAt - totalLifetime); if (tokenAge >= 24 * 60 * 60) { try { const refreshed = await this.helpers.httpRequest({ method: 'GET', url: 'https://graph.instagram.com/refresh_access_token', qs: { grant_type: 'ig_refresh_token', access_token: longLivedToken, }, }); return { longLivedToken: refreshed.access_token, tokenExpiresAt: now + refreshed.expires_in, }; } catch (error) { console.warn('Instagram: Failed to refresh long-lived token, continuing with current token', error); return {}; } } return {}; } if (!shortLivedToken) { return {}; } try { const exchanged = await this.helpers.httpRequest({ method: 'GET', url: 'https://graph.instagram.com/access_token', qs: { grant_type: 'ig_exchange_token', client_secret: clientSecret, access_token: shortLivedToken, }, }); return { longLivedToken: exchanged.access_token, tokenExpiresAt: now + exchanged.expires_in, }; } catch (error) { console.error('Instagram: Failed to exchange OAuth token for long-lived token', error); return {}; } } } exports.InstagramOAuth2Api = InstagramOAuth2Api; //# sourceMappingURL=InstagramOAuth2Api.credentials.js.map