Direct PAGE_ID/feed endpoint update
This commit is contained in:
@@ -84,10 +84,10 @@ async function generatePostWithGemini(topic = 'منشور تعريفي عن من
|
|||||||
* Publish generated post to Meta Facebook Page
|
* Publish generated post to Meta Facebook Page
|
||||||
*/
|
*/
|
||||||
async function publishToFacebook(message) {
|
async function publishToFacebook(message) {
|
||||||
console.log('Publishing generated post to Facebook Page...');
|
console.log(`Publishing generated post to Facebook Page ID: ${META_PAGE_ID}...`);
|
||||||
|
|
||||||
const url = `https://graph.facebook.com/v19.0/me/feed`;
|
const url = `https://graph.facebook.com/v19.0/${META_PAGE_ID}/feed`;
|
||||||
let res = await fetch(url, {
|
const res = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -96,21 +96,7 @@ async function publishToFacebook(message) {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data.error && META_PAGE_ID) {
|
|
||||||
console.log('Direct /me/feed returned error, fallback to Page ID endpoint:', META_PAGE_ID);
|
|
||||||
const fallbackUrl = `https://graph.facebook.com/v19.0/${META_PAGE_ID}/feed`;
|
|
||||||
res = await fetch(fallbackUrl, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
access_token: META_ACCESS_TOKEN,
|
|
||||||
message: message
|
|
||||||
})
|
|
||||||
});
|
|
||||||
data = await res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
throw new Error(`Meta Graph API Error [${data.error.code}]: ${data.error.message}`);
|
throw new Error(`Meta Graph API Error [${data.error.code}]: ${data.error.message}`);
|
||||||
|
|||||||
+3
-18
@@ -36,10 +36,9 @@ const message = `شركتك تحمل اسمك… وتطبيقك يحمل اسم
|
|||||||
💬 تواصل معنا عبر واتساب: https://wa.me/962798583052`;
|
💬 تواصل معنا عبر واتساب: https://wa.me/962798583052`;
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('Publishing post to Facebook Page via Page Token...');
|
console.log(`Publishing post directly to Facebook Page ID: ${PAGE_ID}...`);
|
||||||
|
|
||||||
// Try direct /me/feed endpoint first (works for Page Access Token)
|
const res = await fetch(`https://graph.facebook.com/v19.0/${PAGE_ID}/feed`, {
|
||||||
let res = await fetch(`https://graph.facebook.com/v19.0/me/feed`, {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -48,21 +47,7 @@ async function main() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data.error && PAGE_ID) {
|
|
||||||
console.log('Direct /me/feed failed, trying Page ID endpoint:', PAGE_ID);
|
|
||||||
res = await fetch(`https://graph.facebook.com/v19.0/${PAGE_ID}/feed`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
access_token: USER_TOKEN,
|
|
||||||
message: message
|
|
||||||
})
|
|
||||||
});
|
|
||||||
data = await res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('Response from Meta:', JSON.stringify(data, null, 2));
|
console.log('Response from Meta:', JSON.stringify(data, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user