Facebook SDK v5 Post 作为墙上的页面

Facebook SDK v5 Post as Page on Wall

我有下面的代码片段并且它有效并且post作为我在 Facebook 上的用户帐户发送到 Facebook 页面。

FACEBOOK_* 在代码库中较早定义。

// SDK Version 5.0
$fb = new Facebook\Facebook([
    'app_id' => FACEBOOK_APP_ID,
    'app_secret' => FACEBOOK_APP_SECRET,
    'default_graph_version' => 'v2.4',
]);

// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/'.FACEBOOK_PAGE_ID.'/feed', $postData, FACEBOOK_ACCESS_TOKEN);

$postId = $response->getGraphNode();

现在我的问题是如何将它作为实际页面而不是作为页面管理员的我的帐户获取到 post。

我已经查看了 SDK 文档并且一直在兜圈子,有很多 v4 的示例,但由于它已被弃用,我正在尝试使用 v5,但似乎无法理解结果,我发现任何指向 post 归属或假冒的链接都是 SDK v5 中的死链接。

据我所知,我需要调用 /{user-id}/accounts 以从我的用户 https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

获取页面的访问令牌

但是要获得 {user-id} 我必须查询用户,使用 SDK 文档中的以下示例:

// Make sure to load the Facebook SDK for PHP via composer or manually
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

if($session) {
try {
    $user_profile = (new FacebookRequest(
        $session, 'GET', '/me'
    ))->execute()->getGraphObject(GraphUser::className());
    echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
} 

这里的问题是我不知道如何获得一个会话,我需要获取用户数据,该会话为我提供访问令牌以允许我将访问令牌传递到上面有效的代码片段中,这是如果我没看错的话!?

非常感谢任何帮助!

我和 类 一起工作,所以我根据上面的示例调整了我的代码。测试和工作代码。

使用您使用的方法(请参阅指南 here)获取用户访问令牌后,我们必须获取长期访问令牌。将此添加到您的代码中:

session_start();
$helper = $fb->getRedirectLoginHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // There was an error communicating with Graph
  echo $e->getMessage();
  exit;
}

if (isset($accessToken)) {

    $client = $fb->getOAuth2Client();

    try {
      $accessToken = $client->getLongLivedAccessToken($accessToken);
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
      echo $e->getMessage();
      exit;
    }


  $response = $fb->get('/me/accounts', (string) $accessToken);

    foreach ($response->getDecodedBody() as $allPages) {
        foreach ($allPages as $page ) {               

            if (isset($page['id']) && $page['id'] == $pageId) { // Suppose you save it as this variable
                $appAccessToken = (string) $page['access_token'];
                break;
            }
        }
    }

    $response = $fb->post(
        '/'.$pageId.'/feed',
        array(
            "message" => "Message",
            "link" => "http://www.example.com",
            "picture" => "http://www.example.net/images/example.png",
            "name" => "Title",
            "caption" => "www.example.com",
            "description" => "Description example"
        ),
        $appAccessToken
    );

    // Success
    $postId = $response->getGraphNode();
    echo $postId;

} elseif ($helper->getError()) {
  var_dump($helper->getError());
  var_dump($helper->getErrorCode());
  var_dump($helper->getErrorReason());
  var_dump($helper->getErrorDescription());
  exit;
}

说明:您必须知道您是哪些页面的管理员:

$response = $fb->get('/me/accounts', (string) $accessToken);

然后搜索 table 以检索我们感兴趣的 the access token of the page(我选择了引用页面的 ID)。

最后,简单地运行SDK提供的post功能:

$response = $fb->post(
    '/'.$pageId.'/feed',
    array(
        "message" => "Message",
        "link" => "http://www.example.com",
        "picture" => "http://www.example.net/images/example.png",
        "name" => "Title",
        "caption" => "www.example.com",
        "description" => "Description example"
    ),
    $appAccessToken
);