Question

Is there any way I can get access token of any page by page id, I should be able to post on page behalf of admin?

Was it helpful?

Solution

I'm guessing you don't want to be admin of your clients' pages:

  1. You could create a page so that your clients login to Facebook and grant permissions to your app.

  2. Then you can get the page access token and save it in your DB.

  3. Finally use that access token to post directly to each page as admin without being you the actual admin.

Quoting Facebook documentation:

To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.

To get user access token (supposing you are logged in to fbk and have granted permissions to your App):

    $user = $facebook->getUser();
    if(!$user) {
        $login_url_params = array(
             'scope' => 'publish_stream,read_stream,manage_pages',
             'fbconnect' =>  1,
             'redirect_uri' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
        );
        $login_url = $facebook->getLoginUrl($login_url_params);
        //redirect to the login URL on facebook
        header("Location: {$login_url}");
    }

Don't forget get to a time extended access token

    $facebook->setExtendedAccessToken();
    $access_token = $facebook->getAccessToken();

With that token you can get the page token via the API:

    $accounts = $facebook->api(
       '/me/accounts',
       'GET',
       array(
          'access_token' => $access_token
       )
    );

$accounts will be an associative array with all the info of the pages you admin (including the tokens for each page). To get the token of an specific page knowing its ID:

    $accounts = $accounts['data'];
foreach($accounts as $account){
    if( $account['id'] == $pagId ){
        //$pagId would be the ID of the page you want to use

        //This is the token of the page with the ID $pagId
        $page_access_token = $account['access_token'],
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top