帖子

本指南講解如何使用 Meta 專頁 API,以專頁身分在您的 Facebook 專頁上建立、發佈、更新、回應和刪除帖子。

準備工作

This guide assumes you have read the Overview

For a person who can perform tasks on the page, you will need to implement Facebook Login to ask for the following permissions and receive a Page access token:

  • pages_manage_engagement
  • pages_manage_posts
  • pages_read_engagement
  • pages_read_user_engagement
  • publish_video permission, if you are publishing a video to the Page

Your app user must be able to perform the CREATE_CONTENT, MANAGE, and MODERATE tasks on the Page in the API requests.

If your app users do not own or manage the Page in the API requests, your app will need a User access token and the following features:

  • Page Public Content Access

最佳做法

測試 API 呼叫時,您可以在呼叫中加入 access_token 參數,並將其設為您的存取憑證。但是,從您的應用程式發出安全呼叫時,應使用存取憑證類別

發佈帖子

如要在專頁上發佈帖子,請向 /page_id/feed 端點傳送 POST 要求,其中 page_id 是您的專頁編號。此要求當中應包含以下參數:

  • message 設為帖子文案
  • 如需發佈連結,請將 link 設為有關網址
  • published 設為 true 以立即發佈帖子(預設),或設為 false 以稍後發佈帖子
    • 如此參數設為 false 且日期採用以下其中一種格式,則加入 scheduled_publish_time
      • 以秒為單位的整數 UNIX 時戳,例如:1530432000
      • ISO 8061 格式的時戳字串,例如 2018-09-01T10:15:30+01:00
      • 任何採用其他方式而可由 PHP 的 tomorrow 剖析的字串,例如 strtotime()+2 weeks

有關排定發佈的帖子之備註

  • 發佈日期須為提出 API 要求後的 10 分鐘至 30 日內。
  • 如果您使用 strtotime() 的相對日期字串,可以在寫入後讀取所建立帖子的 scheduled_publish_time,以確保效果符合預期。

要求範例

我們已設定特定格式以便閱讀。請將以粗體及斜體標示的值(如 page_id)替換為您的值。
curl -X POST "https://graph.facebook.com/v19.0/page_id/feed" \
     -H "Content-Type: application/json" \
     -d '{
           "message":"your_message_text",
           "link":"your_url",
           "published":"false",
           "scheduled_publish_time":"unix_time_stamp_of_a_future_date",
         }'

成功的話,您的應用程式就會收到以下 JSON 回應,其中包含帖子編號:

{
  "id": "page_post_id" 
}

新增分享對象目標設定

如要限制哪些用戶可以查看專頁帖子,您可以在 POST 要求中加入 targeting.geo_locations 物件或 feed_targeting.geo_locations 參數。

-d '{
      ...
      "targeting": {
        "geo_locations": {
          "countries": [
            "CA"
          ],
          "cities": [
            {
              "key": "296875",
              "name": "Toronto"
            }
          ]
        }
      },
      ...
    }'

疑難排解

在部分情況下,如果您同時鎖定國家或地區及當地的區域,便會導致以下錯誤:「你有部分地點重疊,請嘗試移除地點。」在這些情況下,請視您要涵蓋的範圍鎖定區域或國家。

發佈媒體帖子

您可以在專頁上發佈相片和影片。

發佈相片

如要在專頁上發佈相片,請向 /page_id/photos 端點傳送 POST 要求,其中 page_id 是您的專頁編號。此要求中的 url 參數應設為帖子相片。

要求範例

我們已設定特定格式以便閱讀。請將以粗體及斜體標示的值(如 page_id)替換為您的值。
curl -X POST "https://graph.facebook.com/v19.0/page_id/photos" \
     -H "Content-Type: application/json" \
     -d '{
           "url":"path_to_photo",

成功的話,您的應用程式就會收到以下 JSON 回應,其中包含相片編號和帖子編號:

{
  "id":"photo_id",
  "post_id":"page_post_id" 
}

發佈影片

請瀏覽影片 API 文件,了解如何在專頁上發佈影片帖子

獲取帖子

如要取得專頁帖子清單,請向 /page_id/feed 端點傳送 GET 要求。

要求範例

我們已設定特定格式以便閱讀。請將以粗體及斜體標示的值(如 page_id)替換為您的值。
curl -i -X GET "https://graph.facebook.com/v19.0/page_id/feed"

成功的話,您的應用程式就會收到以下 JSON 回應,其中的物件陣列會包含在您專頁上所發佈每篇帖子的編號、建立時間和內容:

{
  "data": [
    {
      "created_time": "2019-01-02T18:31:28+0000",
      "message": "This is my test post on my Page.",
      "id": "page_post_id"
    }
  ],
...
}

限制

  • 直播視像:如果專頁帖子包含過期的影片,例如直播,您便只能獲取部分帖子欄位,當中不包括與影片相關的欄位。該影片有其私隱規定。如果影片已過期,只有專頁管理員才能查看其資訊。
  • 訊息 CTA:只要您的應用程式已獲准使用專頁公開內容存取權限功能,便可使用任何存取憑證來要求存取公開分享的專頁帖子。不過,由於專頁無法向其他專頁傳送訊息,您無法使用其他專頁的存取憑證來存取帶有訊息 CTA 的帖子。

專頁帖子網址

專頁帖子的網址或永久連結為 https://www.facebook.com/page_post_id

更新帖子

如要更新專頁帖子,請向 /page_post_id 端點傳送 POST 要求,並將您想更新的參數設為新內容。

要求範例

我們已設定特定格式以便閱讀。請將以粗體及斜體標示的值(如 page_post_id)替換為您的值。
curl -X POST "https://graph.facebook.com/v19.0/page_post_id" \
     -H "Content-Type: application/json" \
     -d '{
           "message":"I am updating my Page post",
         }'

成功的話,您的應用程式就會收到以下 JSON 回應,其中包含設為 true 的 success

{
  "success": true
}

限制

應用程式只能更新以其建立的專頁帖子。

刪除帖子

如要刪除專頁帖子,請向 /page_post_id 端點傳送 DELETE 要求,其中 page_post_id 是您想刪除的帖子所屬編號。

要求範例

我們已設定特定格式以便閱讀。請將以粗體及斜體標示的值(如 page_post_id)替換為您的值。
curl -i -X DELETE "https://graph.facebook.com/v19.0/page_post_id"

成功的話,您的應用程式就會收到以下 JSON 回應,其中包含設為 truesuccess

{
  "success": true
}

後續步驟

了解如何回應專頁帖子和 @提及在您專頁上發佈內容或回應的特定用戶或專頁。