How to post to a Facebook Page (how to get page access token + user access token)

12,598

Solution 1

  1. You can ask the page admin for manage_pages along with offline_access. I do this in my production app to be able to post scheduled postings onto the pages' walls.

  2. Nope. Not possible. That's what asking permissions is all about. And why not everyone gets to administer everyone else's pages. Could you image if you could administer anyone's page without them granting you access?!?

  3. To post to the page as the page, you need a page access token. To post to page's wall as a user, you need a user access token.

  4. Yes, please see: https://developers.facebook.com/docs/reference/api/permissions and https://developers.facebook.com/docs/authentication/

If you have further questions about any one of these, please start a new question. It's not really fair to users of stackoverflow to be hit with 4 questions in one and then to be asked followup questions to each of those.

Solution 2

I have done in Django:

Step to get Page_access_token:

facebook_page_id=360729583957969  
graph = GraphAPI(request.facebook.user.oauth_token.token)                     
page_access_token=graph.get(facebook_page_id+'?fields=access_token')  

This way you can get Page access token.

You can check this thing on Fb GraphAPIexplorer:

http://developers.facebook.com/tools/explorer  
GET URL: fb_page_id?fields=access_token   

for example: 360729583957969?fields=access_token that will give you page_access_token

Share:
12,598
James
Author by

James

Updated on June 06, 2022

Comments

  • James
    James almost 2 years

    I am trying to work out how to post to a Facebook page wall, when using my app as a different Facebook User (who is not the Page Administrator).

    I get a range of error messages while testing: Exception: 200: The user hasn't authorized the application to perform this action

    The page administrator has visited the app and accepted the following permissions: publish_stream, manage_pages, offline_access

    Here is the code I plan to use:

    // Insert Page Administrators ID here 
    // This user is not the same user that is currently logged in and using the app
    // This user is the page administrator who has authorised: 
    // - manage_pages
    // - offline_access
    // - publish_stream
    
    $user_id = '123456789'; 
    
    // Insert Page ID here
    $page_id = '123456789'; 
    
    $accounts = $facebook->api('/'.$user_id.'/accounts');
    
    foreach($accounts['data'] as $account)
    {
      if($account['id'] == $page_id)
      {
        $page_access_token = $account['access_token'];
        echo "<p>Page Access Token: $page_access_token</p>";
      }
    }
    
    // publish to the wall on your page
    try
    {
      $result = $facebook->api(array( "uid" => $page_id, 
                                      "method" => "stream.publish",
                                      "access_token" => $page_access_token,
                                      "message" => $message, ));
    }
    catch (FacebookApiException $e)
    {
      error_log('FB Error: Could not post on Page Wall. Page ID: ' . $page_id);
      error_log('FB Error Message: ' . $e);
    }
    

    Note: There may be PHP errors in the code above, as I just spliced it on the fly, but its not so much the PHP errors I need correcting, but more my logically understanding of how I am meant to go about this process.

    PROBLEM: I can't access the $user_id/accounts information without an active user access token for the Page Administrator.

    The end result that I'm trying to achieve is: 1.) A normal FB user goes to the app and submits a form 2.) The app posts a message on a FB Page wall, which is not owned by the FB user, but has previously been authorized by the Page Administrator with the following permissions manage_pages, publish_stream and offline_access

    Q1. Since the Page Administrator has accepted the appropriate permissions, why can't I just generate an active user access token, without the actual Page Administrator user logging into the website?

    Q2. Is there a way I can get the equivalent of /$user_id/accounts for the Page Administrator user_id, when logged into Facebook as a different user (which is why I do not use /me/accounts)?

    Q3. Please confirm that my understanding of needing the page access token to post to the page wall is correct (or do I need the user access_token for the Page Administrator - see Q1)?

    Q4. Anyone have a handy resource on what each type of access_token can actually access?

    If you need any more information, please let me know. I've spent the last few days working on this and I'm stuck.

    Thanks!