Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

How can I get page token and post on page as a page #785

Closed
severin-bruhat opened this issue May 18, 2017 · 3 comments
Closed

How can I get page token and post on page as a page #785

severin-bruhat opened this issue May 18, 2017 · 3 comments

Comments

@severin-bruhat
Copy link

Hi,

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

Cheers

@tolbon
Copy link
Contributor

tolbon commented May 19, 2017

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

@severin-bruhat
Copy link
Author

Thanks,

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

Thx

@severin-bruhat
Copy link
Author

severin-bruhat commented May 23, 2017

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants