使用api在facebook页面上发布图片或状态

post image or status with facebook page with api

本文关键字:布图片 状态 api facebook 使用      更新时间:2023-09-26

从昨天开始,我尝试用带有facebook图形api的facebook页面发布带有图像(或不带有图像)的状态。

客户端连接到此范围:

电子邮件、public_profile、manage_pages、user_posts、publish_action、publish_pages

我把这个函数称为

    postPicture = () => {
        FB.api("/231713458314438/feed",
                "POST", 
                {
                    "link": "http://ichef-1.bbci.co.uk/news/660/cpsprodpb/17A21/production/_85310869_85310700.jpg",
                    "message": "test"
                },
                function (response) {
                    console.log(response)
                }
        );
    }

出版物已发布,但我的个人帐户在页面上,而不是在管理页面上。

所以你知道如何用页面发布状态吗?

根据此处的文档:https://developers.facebook.com/docs/graph-api/overview/

您还需要将为登录的管理员生成的access_token作为参数请求传递:

postPicture = () => {
    FB.api("/231713458314438/feed",
            "POST", 
            {
                "link": "http://ichef-1.bbci.co.uk/news/660/cpsprodpb/17A21/production/_85310869_85310700.jpg",
                "message": "test",
                "access_token": {your-access-token}
            },
            function (response) {
                console.log(response)
            }
    );
}
  1. 从Facebook门户网站获取用户访问令牌

    • https://developers.facebook.com/tools/explorer

      选择";Facebook应用程序":你的应用

      选择";用户或页面":用户令牌(获取用户访问令牌)

      选择权限:"pages_manage_posts";

      点击复制访问令牌:

      • =>短期用户访问令牌:SLUATOKEN
  2. 获取长期用户访问令牌

    • https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing#get-a长期用户访问

      转到https://developers.facebook.com/apps/

      选择应用程序->设置->基本->获取应用程序ID,应用程序机密

      curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?  
          grant_type=fb_exchange_token&          
          client_id={app-id}&
          client_secret={app-secret}&
          fb_exchange_token={Short-Lived User Access Token}"
      

      =>卷曲:

      curl -i -X GET "https://graph.facebook.com/v10.0/oauth/access_token?grant_type=fb_exchange_token&client_id=APPID&client_secret=APPSECRET&fb_exchange_token=SLUATOKEN"
      
      • =>获得长期用户访问令牌:LONGLIVEDUATOKEN
  3. 获取用户ID

    • 步骤4 中需要用户ID

      curl -i -X GET "https://graph.facebook.com/v10.0/me?fields=id%2Cname&access_token={Long-Live User Access Token}"
      

      =>卷曲:

      curl -i -X GET "https://graph.facebook.com/v10.0/me?fields=id%2Cname&access_token=LONGLIVEDUATOKEN"
      
      • =>已获取用户ID:YOURFBID
  4. 获取长寿页面令牌

    • https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing#get-a-长时间的页面访问

      curl -i -X GET "https://graph.facebook.com/{graph-api-version}/{user-id}/accounts?access_token={Long-Lived User Access Token}"
      

      =>卷曲:

      curl -i -X GET "https://graph.facebook.com/v10.0/YOURFBID/accounts?access_token=LONGLIVEDUATOKEN"
      
      • =>获得长寿页面令牌:LONGLIVEDPAGEACCESSTOKEN
  5. 张贴状态

    • https://developers.facebook.com/docs/graph-api/reference/page/feed/#publish

      =>卷曲:

      curl -i -X POST "https://graph.facebook.com/v10.0/feed?message=This_Is_My_New_Status&access_token=LONGLIVEDPAGEACCESSTOKEN"
      
      • =>完成!查看你的Facebook页面
  6. 检查令牌过期时间

    • https://developers.facebook.com/tools/debug/accesstoken/?access_token=LONGLIVEDPAGEACCESSTOKEN&版本=v10.0
      • 您将看到:Expires: Never