Open samtipton opened 8 months ago
jquery version
// Replace with your app details and page ID
const appId = 'your-app-id';
const appSecret = 'your-app-secret';
const pageId = 'your-page-id';
let userAccessToken = 'your-user-access-token';
// Get an access token with the required permissions
const getAccessToken = () => {
return $.get({
url: `https://graph.facebook.com/v12.0/oauth/access_token`,
data: {
grant_type: 'fb_exchange_token',
client_id: appId,
client_secret: appSecret,
fb_exchange_token: userAccessToken,
}
});
};
// Get the most recent live video post
const getRecentLiveVideo = () => {
return $.get({
url: `https://graph.facebook.com/v12.0/${pageId}/live_videos`,
data: {
fields: 'id,description,embed_html,status',
limit: 1,
access_token: userAccessToken,
}
});
};
// Main execution
$(document).ready(() => {
getAccessToken()
.then(() => getRecentLiveVideo())
.then((liveVideoData) => {
const liveVideo = liveVideoData.data[0];
if (liveVideo) {
const embedHtml = liveVideo.embed_html;
$('#live-video-container').html(embedHtml);
} else {
$('#live-video-container').html('No live videos found on the page.');
}
})
.catch((error) => {
console.error('Error:', error.responseJSON || error.statusText || error);
});
});
Current issue to solve is how to store facebook api secret.
Can use the Facebook Graph API to get the most recent streaming video and grab its embed source html.
Create a Facebook App:
Get an Access Token:
pages_show_list
andpages_read_engagement
permissions. You can use the Graph API Explorer for this purpose.Find Page ID:
Use the API to Get Live Videos:
{page-id}
with the actual Page ID.Retrieve Embed HTML:
embed_html
field from the live video post.Generate Embedded Video Player:
embed_html
to generate the embedded video player on your webpage.Typescript code:
Can show widget only during streaming normal hours Sundays 9-12, Wednesdays 7-8:30