简体   繁体   English

Facebook SDK v5发布为Wall on Wall

[英]Facebook SDK v5 Post as Page on Wall

I have the below snippet and it works and posts to a Facebook page as my own user account on Facebook. 我有以下代码段,它可以作为我在Facebook上的用户帐户发布到Facebook页面。

The values FACEBOOK_* are defined earlier in the codebase. 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();

Now my question is how can I get it to post as the actual page and not my account which is the admin of the page. 现在我的问题是我如何才能将其发布为实际页面,而不是作为页面管理员的我的帐户。

I've had a look at the SDK documentation and I've been going around in circles, there are many examples of v4 but as it's deprecated I'm trying to use v5 and just can't seem to figure it out, any links to post attribution or impersonation I find are dead links in v5 of the SDK. 我看过SDK文档,并且一直到处逛逛,有很多v4的例子,但是由于不推荐使用,所以我正在尝试使用v5,但似乎无法弄清楚,任何链接我发现发布归因或假冒是SDK v5中的无效链接。

From what I can see I need to make a call to /{user-id}/accounts to get an access token for the page from my user, https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens 从我看到的信息中,我需要致电/ {user-id} / accounts以从我的用户https://developers.facebook.com/docs/facebook-login/access-获取用户对该页面的访问令牌。 令牌#page令牌

But to get a {user-id} I have to query the user, with something like the below example from the SDK documentation: 但是要获得{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();
} 

The issue here is that I have no idea how to get a session which I need to get the user data for which gives me the access token to allow me pass the access token into my code snippet above that works, that's if I understand it all correctly!? 这里的问题是我不知道如何获取我需要获取用户数据的会话,该会话为我提供了访问令牌,以允许我将访问令牌传递到上面的代码片段中,即我能理解正确!!

Any help greatly appreciated! 任何帮助,不胜感激!

I work with classes, so I adapted my code to your examples above. 我使用类,因此我将代码修改为上面的示例。 Tested and working code. 经过测试的工作代码。

After getting your user access token using the method you use (see the guide here ), we have to obtain a long-lived access token. 使用您使用的方法获得用户访问令牌后(请参阅此处的指南),我们必须获取一个长期存在的访问令牌。 Add this to your code : 将此添加到您的代码:

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

Explanations : You have to know which pages you are administrator : 说明 :您必须知道您是哪些页面的管理员:

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

Then search the table to retrieve the access token of the page that interests us (I have chosen to take the id of the page referenced). 然后搜索表以检索我们感兴趣the access token of the page (我已选择获取所引用页面的ID)。

Finally, simply run the post function provided by the SDK : 最后,只需运行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