← back to Marketing Command Center
scripts/probe-business-discovery.js
38 lines
#!/usr/bin/env node
// READ-ONLY probe: can META_ACCESS_TOKEN do Instagram business_discovery?
// 1) find a DW-owned IG business account id via /me/accounts
// 2) try business_discovery for one vendor handle -> last 3 posts
const fs = require('fs');
const path = require('path');
const GRAPH = 'https://graph.facebook.com/v23.0';
const env = fs.readFileSync(path.join(__dirname, '..', '.env'), 'utf8');
const TOKEN = (env.match(/^META_ACCESS_TOKEN=(.+)$/m) || [])[1];
if (!TOKEN) { console.error('no META_ACCESS_TOKEN'); process.exit(1); }
const TEST_HANDLE = process.argv[2] || '1838_wallcoverings';
(async () => {
// 1) pages + IG business accounts
let r = await fetch(`${GRAPH}/me/accounts?fields=name,instagram_business_account{id,username}&limit=100&access_token=${encodeURIComponent(TOKEN)}`);
let j = await r.json();
if (j.error) { console.error('me/accounts error:', JSON.stringify(j.error)); process.exit(1); }
const pages = (j.data || []);
const withIG = pages.filter(p => p.instagram_business_account);
console.log(`Pages: ${pages.length} | with IG business account: ${withIG.length}`);
if (!withIG.length) { console.error('NO IG business account linked — business_discovery impossible with this token.'); process.exit(2); }
const ig = withIG[0].instagram_business_account;
console.log(`Using discovering IG: @${ig.username} (id ${ig.id}) via Page "${withIG[0].name}"`);
// 2) business_discovery for the test vendor
const fields = `business_discovery.username(${TEST_HANDLE}){username,followers_count,media_count,media.limit(3){id,caption,media_type,media_url,thumbnail_url,permalink,timestamp,like_count,comments_count}}`;
r = await fetch(`${GRAPH}/${ig.id}?fields=${encodeURIComponent(fields)}&access_token=${encodeURIComponent(TOKEN)}`);
j = await r.json();
if (j.error) { console.error(`business_discovery error for ${TEST_HANDLE}:`, JSON.stringify(j.error)); process.exit(3); }
const bd = j.business_discovery;
console.log(`\n✅ business_discovery WORKS for @${bd.username}: ${bd.followers_count} followers, ${bd.media_count} posts`);
console.log('Last 3 media:');
for (const m of (bd.media?.data || [])) {
console.log(` ${m.timestamp?.slice(0,10)} ${m.media_type} ♥${m.like_count ?? '?'} 💬${m.comments_count ?? '?'} ${m.permalink}`);
console.log(` img: ${(m.media_url || m.thumbnail_url || '').slice(0,80)}`);
}
})().catch(e => { console.error('FATAL', e.message); process.exit(1); });