Last active
November 20, 2025 21:08
-
-
Save moelzanaty3/8e6abeb8f7f10585e52fa3471dd62dac to your computer and use it in GitHub Desktop.
generate token from instgram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| /** | |
| * Instagram Token Refresher (CommonJS version) | |
| * Safer to run on standard server setups | |
| */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const https = require('https'); // Native node module, no extra npm install needed | |
| // Defines where the token lives | |
| const storeFile = path.join(__dirname, 'token-store.json'); | |
| // Helper function to fetch without installing 'node-fetch' | |
| function httpsGet(url) { | |
| return new Promise((resolve, reject) => { | |
| https.get(url, (res) => { | |
| let data = ''; | |
| res.on('data', (chunk) => data += chunk); | |
| res.on('end', () => resolve(JSON.parse(data))); | |
| }).on('error', (err) => reject(err)); | |
| }); | |
| } | |
| async function refresh() { | |
| try { | |
| // 1. Read the old token | |
| if (!fs.existsSync(storeFile)) { | |
| console.error('❌ token-store.json not found'); | |
| process.exit(1); | |
| } | |
| const store = JSON.parse(fs.readFileSync(storeFile, 'utf-8')); | |
| const oldToken = store.access_token; | |
| if (!oldToken) { | |
| console.error('❌ No token inside JSON file'); | |
| process.exit(1); | |
| } | |
| // 2. Call Instagram API | |
| // NOTE: This URL is for Basic Display. | |
| // If using Business API, this URL needs to change. | |
| const url = `https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=${oldToken}`; | |
| const data = await httpsGet(url); | |
| if (data.error || !data.access_token) { | |
| console.error('❌ API Error:', data); | |
| return; | |
| } | |
| // 3. Save the new token | |
| store.access_token = data.access_token; | |
| store.expires_in = data.expires_in; | |
| store.last_refreshed = new Date().toISOString(); | |
| fs.writeFileSync(storeFile, JSON.stringify(store, null, 2)); | |
| console.log(`✅ Success! Token refreshed. Expires in ~${Math.round(data.expires_in / 86400)} days.`); | |
| } catch (err) { | |
| console.error('❌ Script Error:', err); | |
| } | |
| } | |
| refresh(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment