Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: facebook and instagram connect

Nevo David16713f03b688
diff --git a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
index ccad72e75..0050131e7 100644
--- a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
@@ -262,13 +262,64 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider {
}
async pages(accessToken: string) {
- const { data } = await (
- await fetch(
- `https://graph.facebook.com/v20.0/me/accounts?fields=id,username,name,picture.type(large)&access_token=${accessToken}`
- )
- ).json();
+ const seenIds = new Set<string>();
+ const allPages: any[] = [];
+
+ const fetchPaginated = async (startUrl: string) => {
+ let nextUrl: string | undefined = startUrl;
+ while (nextUrl) {
+ const response = await (await fetch(nextUrl)).json();
+ if (response.data) {
+ for (const page of response.data) {
+ if (!seenIds.has(page.id)) {
+ seenIds.add(page.id);
+ allPages.push(page);
+ }
+ }
+ }
+ nextUrl = response.paging?.next;
+ }
+ };
+
+ // Fetch pages the user explicitly shared during the OAuth dialog
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/me/accounts?fields=id,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+
+ // Also fetch pages via Business Manager API to discover pages
+ // not selected during the OAuth page selection step
+ try {
+ let bizUrl: string | undefined =
+ `https://graph.facebook.com/v20.0/me/businesses?access_token=${accessToken}`;
+
+ while (bizUrl) {
+ const bizResponse = await (await fetch(bizUrl)).json();
+ if (bizResponse.data) {
+ for (const business of bizResponse.data) {
+ try {
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/${business.id}/owned_pages?fields=id,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+ } catch {
+ // Continue with other businesses
+ }
+
+ try {
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/${business.id}/client_pages?fields=id,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+ } catch {
+ // Continue with other businesses
+ }
+ }
+ }
+ bizUrl = bizResponse.paging?.next;
+ }
+ } catch {
+ // Business Manager API not available for all users
+ }
- return data;
+ return allPages;
}
async fetchPageInformation(accessToken: string, data: { page: string }) {
diff --git a/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts b/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts
index 47fd7d912..435d23e94 100644
--- a/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts
@@ -412,21 +412,72 @@ export class InstagramProvider
}
async pages(accessToken: string) {
- const { data } = await (
- await fetch(
- `https://graph.facebook.com/v20.0/me/accounts?fields=id,instagram_business_account,username,name,picture.type(large)&access_token=${accessToken}&limit=500`
- )
- ).json();
+ const seenPageIds = new Set<string>();
+ const allFacebookPages: any[] = [];
+
+ const fetchPaginated = async (startUrl: string) => {
+ let nextUrl: string | undefined = startUrl;
+ while (nextUrl) {
+ const response = await (await fetch(nextUrl)).json();
+ if (response.data) {
+ for (const page of response.data) {
+ if (!seenPageIds.has(page.id)) {
+ seenPageIds.add(page.id);
+ allFacebookPages.push(page);
+ }
+ }
+ }
+ nextUrl = response.paging?.next;
+ }
+ };
+
+ // Fetch pages the user explicitly shared during the OAuth dialog
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/me/accounts?fields=id,instagram_business_account,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+
+ // Also fetch pages via Business Manager API to discover pages
+ // not selected during the OAuth page selection step
+ try {
+ let bizUrl: string | undefined =
+ `https://graph.facebook.com/v20.0/me/businesses?access_token=${accessToken}`;
+
+ while (bizUrl) {
+ const bizResponse = await (await fetch(bizUrl)).json();
+ if (bizResponse.data) {
+ for (const business of bizResponse.data) {
+ try {
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/${business.id}/owned_pages?fields=id,instagram_business_account,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+ } catch {
+ // Continue with other businesses
+ }
+
+ try {
+ await fetchPaginated(
+ `https://graph.facebook.com/v20.0/${business.id}/client_pages?fields=id,instagram_business_account,username,name,picture.type(large)&limit=100&access_token=${accessToken}`
+ );
+ } catch {
+ // Continue with other businesses
+ }
+ }
+ }
+ bizUrl = bizResponse.paging?.next;
+ }
+ } catch {
+ // Business Manager API not available for all users
+ }
const onlyConnectedAccounts = await Promise.all(
- data
+ allFacebookPages
.filter((f: any) => f.instagram_business_account)
.map(async (p: any) => {
return {
pageId: p.id,
...(await (
await fetch(
- `https://graph.facebook.com/v20.0/${p.instagram_business_account.id}?fields=name,profile_picture_url&access_token=${accessToken}&limit=500`
+ `https://graph.facebook.com/v20.0/${p.instagram_business_account.id}?fields=name,profile_picture_url&access_token=${accessToken}`
)
).json()),
id: p.instagram_business_account.id,