fix: Meta sync usa token agency_oauth (no solo env BPS)

El META_ACCESS_TOKEN de Dokploy solo tenía acceso a BPS; el de agency_oauth cubre el resto. Probamos candidatos y priorizamos agency.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-29 17:33:17 +02:00
parent 1a58af129d
commit c11ec20f26
2 changed files with 39 additions and 13 deletions

View File

@@ -37,11 +37,13 @@ export async function resolveMetaAccessToken(): Promise<string | null> {
return tokens[0] || null;
}
/** All candidate Meta tokens (env first — production 4V2030 system user). */
/**
* Candidate Meta tokens.
* Prefer agency_oauth (system user with BM access to client ad accounts);
* Dokploy META_ACCESS_TOKEN may only reach a subset (historically BPS).
*/
export async function resolveMetaAccessTokens(): Promise<string[]> {
const out: string[] = [];
const env = process.env.META_ACCESS_TOKEN?.trim();
if (env) out.push(env);
const headers = serviceHeaders();
if (headers) {
@@ -53,12 +55,16 @@ export async function resolveMetaAccessTokens(): Promise<string[]> {
if (r.ok) {
const rows = (await r.json()) as { access_token?: string }[];
const t = rows[0]?.access_token?.trim();
if (t && !out.includes(t)) out.push(t);
if (t) out.push(t);
}
} catch {
/* ignore */
}
}
const env = process.env.META_ACCESS_TOKEN?.trim();
if (env && !out.includes(env)) out.push(env);
return out;
}