簡體   English   中英

Facebook SDK v5發布為Wall on Wall

[英]Facebook SDK v5 Post as Page on Wall

我有以下代碼段,它可以作為我在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();

現在我的問題是我如何才能將其發布為實際頁面,而不是作為頁面管理員的我的帳戶。

我看過SDK文檔,並且一直到處逛逛,有很多v4的例子,但是由於不推薦使用,所以我正在嘗試使用v5,但似乎無法弄清楚,任何鏈接我發現發布歸因或假冒是SDK v5中的無效鏈接。

從我看到的信息中,我需要致電/ {user-id} / accounts以從我的用戶https://developers.facebook.com/docs/facebook-login/access-獲取用戶對該頁面的訪問令牌。 令牌#page令牌

但是要獲得{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();
} 

這里的問題是我不知道如何獲取我需要獲取用戶數據的會話,該會話為我提供了訪問令牌,以允許我將訪問令牌傳遞到上面的代碼片段中,即我能理解正確!!

任何幫助,不勝感激!

我使用類,因此我將代碼修改為上面的示例。 經過測試的工作代碼。

使用您使用的方法獲得用戶訪問令牌后(請參閱此處的指南),我們必須獲取一個長期存在的訪問令牌。 將此添加到您的代碼:

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);

然后搜索表以檢索我們感興趣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
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM