Php-graph-sdk: How can I get page token and post on page as a page

Created on 18 May 2017  路  3Comments  路  Source: facebookarchive/php-graph-sdk

Hi,

I think everything is in the title ;)
How can I get page token and post on page as a page ?

Cheers

Most helpful comment

I have just found the solution. Here it is

        $fb = new \Facebook\Facebook([
            'app_id' => 'XXXXXXXXXXX', //TODO  change
            'app_secret' => 'yyyyyyyyyyyyyyyyyyy', //TODO  change
            'default_graph_version' => 'v2.9',
        ]);
    $linkData = [
        'link' => 'http://google.com', //TODO  change
        'message' => "My message", //TODO  change
    ];

    try {
      $pageId = '0000000000'; //id of the page you want to publish on
      $longLivedToken = $fb->getOAuth2Client()->getLongLivedAccessToken('user_access_token'); //TODO  change
      $fb->setDefaultAccessToken($longLivedToken);
      $response = $fb->sendRequest('GET', $pageId, ['fields' => 'access_token'])
          ->getDecodedBody();

      $foreverPageAccessToken = $response['access_token'];
      $fb->setDefaultAccessToken($foreverPageAccessToken);
      $response = $fb->post('/$pageId/feed', $linkData);

All 3 comments

If you read the documentation
https://developers.facebook.com/docs/graph-api/reference/page/feed#publish
You see :

A page access token with publish_pages permission can be used to publish new posts on behalf of that page. Posts will appear in the voice of the page.

So if add permission manage_page & publish_pages. And when user accept your app you request for
{USER_ID}/accounts

and you see page have access_token variable so you use this value when you publish

Thx

Thanks,

But how can I get the page access token with the API so I can pass it to the function $fb->post() ?

Thx

I have just found the solution. Here it is

        $fb = new \Facebook\Facebook([
            'app_id' => 'XXXXXXXXXXX', //TODO  change
            'app_secret' => 'yyyyyyyyyyyyyyyyyyy', //TODO  change
            'default_graph_version' => 'v2.9',
        ]);
    $linkData = [
        'link' => 'http://google.com', //TODO  change
        'message' => "My message", //TODO  change
    ];

    try {
      $pageId = '0000000000'; //id of the page you want to publish on
      $longLivedToken = $fb->getOAuth2Client()->getLongLivedAccessToken('user_access_token'); //TODO  change
      $fb->setDefaultAccessToken($longLivedToken);
      $response = $fb->sendRequest('GET', $pageId, ['fields' => 'access_token'])
          ->getDecodedBody();

      $foreverPageAccessToken = $response['access_token'];
      $fb->setDefaultAccessToken($foreverPageAccessToken);
      $response = $fb->post('/$pageId/feed', $linkData);
Was this page helpful?
0 / 5 - 0 ratings