如何获取Facebook主页实时直播流并嵌入教会应用
Hey there! Let's break down how to solve this problem for your church's app—focused solely on grabbing active Facebook Live streams from your church's page, no old videos needed. Here's a step-by-step guide tailored to your needs:
Before diving into code/API calls, you'll need to set up a few things on Facebook's Developer Platform:
- Create a Facebook Developer account and register a new app (select the "Business" type since this is for a church page).
- Request the necessary permissions for your app:
pages_show_list: To access your church's Facebook page details.pages_read_engagement: To read page content including live video statuses.pages_read_user_content(if needed): For accessing more detailed live video metadata.
- Generate a long-lived Page Access Token: Short-lived tokens expire quickly, so convert a short-lived user token to a 60-day user token, then swap that for a permanent Page token (this won't expire unless your church revokes app access).
You have two reliable ways to know when a live stream goes live—pick the one that fits your app's complexity:
Option 1: Webhooks (Recommended for Real-Time Alerts)
This is the most efficient method (no constant polling) and will notify your app instantly when a live stream starts:
- In your Facebook app dashboard, navigate to Webhooks > Add Product > Select "Pages" as the subscription object.
- Set up a public endpoint on your server that can receive HTTP POST requests (Facebook will send a verification request first to confirm this endpoint is valid).
- Subscribe to the
live_videofield, specifically theupdateevent. When your church's page starts a live stream, Facebook will send a payload to your endpoint with the live video's ID and status (marked asLIVE).
Option 2: Periodic Polling (Simpler for Small Apps)
If setting up Webhooks feels too heavy, you can periodically check the page for active live streams:
- Call the Graph API endpoint at regular intervals (e.g., every 1-5 minutes) to fetch only live streams:
curl -X GET "https://graph.facebook.com/v18.0/{YOUR_CHURCH_PAGE_ID}/live_videos?status=LIVE&access_token={YOUR_PAGE_ACCESS_TOKEN}" - If the response returns a non-empty
dataarray, that means a live stream is active—grab theidof the live video from the response.
Once you have the active live video's ID, call the Graph API to get the real-time stream URL:
curl -X GET "https://graph.facebook.com/v18.0/{LIVE_VIDEO_ID}?fields=stream_url&access_token={YOUR_PAGE_ACCESS_TOKEN}"
The stream_url field in the response is an HLS (HTTP Live Streaming) URL that you can embed directly into your church's app using a video player that supports HLS (most modern mobile/web players do).
- Stream URL Expiry: The
stream_urlis temporary and may refresh during a long stream—if your app needs to maintain the stream for hours, consider re-fetching the URL every 30-60 minutes. - API Version: Always use the latest Graph API version (e.g., v18.0 at the time of writing) to avoid deprecated endpoints.
- Permission Checks: If you get empty responses or errors, double-check that your Page access token has all the required permissions and hasn't expired.
内容的提问来源于stack exchange,提问作者Fábio Monteiro

