3

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!

James
  • 41
  • 1
  • 1
  • 4

2 Answers2

3
  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.

DMCS
  • 31,720
  • 14
  • 71
  • 104
  • 1. I already ask for this permission and can confirm that the Page Administrator has this permission enabled for my app 2. I understand the need for permissions, but my app will be installed on the clients Facebook Page, and as users create new items, I'm wanting to post on the clients Facebook Page wall. So at the time that a message is attempted to be posted, the user that is using the app is not the Page administrator, but just an everyday user. – James Jan 19 '12 at 23:31
  • 3. Thanks for clarifying the purpose of what the Page Access Token is used for. It sounds as though I should be able to use the current users access token to post to a page wall (owned by the page administrator - who has approved the appropriate permissions - manage_pages/offline_access) but I get authentication errors (which I'll track down and update my post) 4. I have read these pages inside out and don't seem to be having any luck with getting the answers I need. Error Log: Uncaught Exception: 240: Requires a valid user is specified – James Jan 19 '12 at 23:31
  • 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. – DMCS Jan 19 '12 at 23:49
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

Ron
  • 22,128
  • 31
  • 108
  • 206
Invincible
  • 412
  • 3
  • 19