Update publish-now script with auto Page Token resolution
This commit is contained in:
+22
-3
@@ -3,8 +3,8 @@
|
|||||||
* Direct Live Publishing Script for Facebook Page
|
* Direct Live Publishing Script for Facebook Page
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const ACCESS_TOKEN = 'EAAM8GXpb76wBSLELO5tWBvXM2NG6aSsZBjHUHKK35nATiw5GFgyEAJ5WoiK68NudGqpGlcl2mZB6bUpR5KbtSFPzIpaANhbpXsjDWo8UFCNbWOHKcnGcXM20G4umUoLsoyBNYg9la80wTEhVHFmQmubp7q7ZCVZCrec4BnzXk5iGDMZC38JJOZBRKFMMJHdwthy7';
|
const USER_TOKEN = process.env.META_ACCESS_TOKEN || 'EAAM8GXpb76wBSLELO5tWBvXM2NG6aSsZBjHUHKK35nATiw5GFgyEAJ5WoiK68NudGqpGlcl2mZB6bUpR5KbtSFPzIpaANhbpXsjDWo8UFCNbWOHKcnGcXM20G4umUoLsoyBNYg9la80wTEhVHFmQmubp7q7ZCVZCrec4BnzXk5iGDMZC38JJOZBRKFMMJHdwthy7';
|
||||||
const PAGE_ID = '61588173848643';
|
const PAGE_ID = process.env.META_PAGE_ID || '61588173848643';
|
||||||
|
|
||||||
const message = `شركتك تحمل اسمك… وتطبيقك يحمل اسم غيرك؟ 🚘📱
|
const message = `شركتك تحمل اسمك… وتطبيقك يحمل اسم غيرك؟ 🚘📱
|
||||||
|
|
||||||
@@ -18,14 +18,33 @@ const message = `شركتك تحمل اسمك… وتطبيقك يحمل اسم
|
|||||||
📊 احسب توفيرك السنوي: https://tripz-egypt.com/apps/
|
📊 احسب توفيرك السنوي: https://tripz-egypt.com/apps/
|
||||||
💬 تواصل معنا عبر واتساب: https://wa.me/962798583052`;
|
💬 تواصل معنا عبر واتساب: https://wa.me/962798583052`;
|
||||||
|
|
||||||
|
async function getPageAccessToken(userToken, targetPageId) {
|
||||||
|
try {
|
||||||
|
const url = `https://graph.facebook.com/v19.0/me/accounts?access_token=${userToken}`;
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.data && Array.isArray(data.data)) {
|
||||||
|
const pageObj = data.data.find(p => p.id === targetPageId) || data.data[0];
|
||||||
|
if (pageObj && pageObj.access_token) {
|
||||||
|
console.log(`Found Page Access Token for Page: ${pageObj.name} (${pageObj.id})`);
|
||||||
|
return pageObj.access_token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
return userToken;
|
||||||
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log('Publishing post to Facebook Page', PAGE_ID, '...');
|
console.log('Publishing post to Facebook Page', PAGE_ID, '...');
|
||||||
|
|
||||||
|
const tokenToUse = await getPageAccessToken(USER_TOKEN, PAGE_ID);
|
||||||
|
|
||||||
const url = `https://graph.facebook.com/v19.0/${PAGE_ID}/feed`;
|
const url = `https://graph.facebook.com/v19.0/${PAGE_ID}/feed`;
|
||||||
const 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({
|
||||||
access_token: ACCESS_TOKEN,
|
access_token: tokenToUse,
|
||||||
message: message
|
message: message
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user