0

I can't find an example of exactly what I'm trying to do, so sorry if this is a repeated question, but I have looked for the last couple of hours.

I want my Facebook app to be able to update a status or post to a wall of a page that I administrate: http://www.facebook.com/amazingjobsapp as part of a CRON job.

I just can't seem to find any examples on how to do this. Any pointers are greatly appreciated.

EDIT:

I've used to code below and now am getting:

object(FacebookApiException)#11 (8) { ["result":protected]=> array(1) { ["error"]=> array(3) { ["message"]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["type"]=> string(14) "OAuthException" ["code"]=> int(200) } } ["message":protected]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(86)

Can someone walk me through granting my app manage_page and publish_stream permissions for page id 413282938687554 please?

EDIT 2: If I look at my graph.facebook.com/me/accounts then it shows the page id above with an access token but if I use that in my code then I still get the error.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64

4 Answers4

2

You need to get the manages_pages permission

<?php
$attachment = array(
    'message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => "Caption of the Post",
    'link' => 'http://mylink.com',
    'description' => 'this is a description',
    'picture' => 'http://mysite.com/pic.gif',
    'actions' => array(
        array(
            'name' => 'Get Search',
            'link' => 'http://www.google.com'
        )
    )
);

// To authenticated user's wall
$result = $facebook->api('/me/feed/', 'post', $attachment);

// To page's wall
$result = $facebook->api('page-id/feed/','post',$attachment);

// To user's wall
$result = $facebook->api('user-id/feed/','post',$attachment);
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
2

Ok maybe I missed something in the docs but I found the solution with is as follows:

Note this is for people who are trying to post from a cron job.

  • Grant the manage_pages, publish_stream and offline_access perms to your app
  • Get the access token for your page from me/accounts in the graph api explorer
  • Add one simple line of code just after you initialize your fb object

    $facebook->setAccessToken("XXXXX");
    

Note the access token needs to be the PAGE access token you got from step 2 and remember if you de-auth the app the access token will change.

Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • `offline_access` is deprecated – see https://developers.facebook.com/roadmap/offline-access-removal/ – CBroe Aug 09 '12 at 11:39
  • This is also necessary for posting from php. I tried /feed?access_token=XXXXX, but that didn't work. But setAccessToken did. Why is this not mentioned anywhere else? Even answers to similar questions a year later don't address this. – mutatron Jan 09 '14 at 21:33
0
  1. Application will need to ask you for manage_pages & publish_stream permissions.
  2. Obtain your pages page_access_token from the graph api /accounts call. Each page you own will have its own token listed. This token can be saved to database or accessed at the time of need. It is not recommended to expose or share this token.
  3. Use "php-sdk" to make the post.

refer to: http://developers.facebook.com/docs/authentication/

refer to: http://developers.facebook.com/docs/reference/php/

NOTE: To impersonate the Page when posting to the wall (i.e. post as the Page, and not the current user), you must use a Page access_token with the manage_pages and publish_stream permissions, as described under Page Access Tokens above.

Example: Assumes user has installed and initialized php sdk for Facebook


<?php
if($user){
  $vars = array(
  'message' => "message goes here",
  // you can uncomment below to enable an image with caption and link.
  //'picture' => "image",
  //'link' => "Link here",
  //'name' => "Name here",
  //'caption' => "Caption here",
  'show_error' => true,
  // comment out the access_token array below to post as a user instead of as page.
  array('access_token' => YourPageAccessToken)
);
$testpost = $facebook->api('YourPageId/feed', 'post', $vars);
}
?>

If you find this answer helpful or is correct please remember to mark it up or mark it as correct, so future users will have an easier time finding answers.

ShawnDaGeek
  • 4,145
  • 1
  • 22
  • 39
  • Thanks Shawn, I know it seems dim but I just couldn't get my head round it - can I not physically just grant this permission rather than having to code for it? – Mrk Fldig Aug 07 '12 at 19:55
  • yes, use the graph api explorer, on the right use the drop down to select you app then get access token, a permissions dialog will appear and you can grant that way. http://developers.facebook.com/tools/explorer?method=GET&path=171535666239724 – ShawnDaGeek Aug 07 '12 at 20:02
  • Hi shawn thanks for that, sorry for being dim but I manage a couple of pages how do I ensure the post goes to the right page? – Mrk Fldig Aug 07 '12 at 20:15
  • ah sorry so $testpost = $facebook->api('dialog/feed', 'post', $vars); should be $testpost = $facebook->api('amazingjobsapp/feed', 'post', $vars); – Mrk Fldig Aug 07 '12 at 20:21
  • yes sorry i copy pasted a snippet from my app i forgot to edit that. i use a popup in my app. – ShawnDaGeek Aug 07 '12 at 20:22
  • So if I pull my pages using https://graph.facebook.com/me/accounts and then use the access token thats on there will that access token change at all? The problem with this is that the script runs as a cron job so there won't actually be a user logged in at all while this is going on – Mrk Fldig Aug 07 '12 at 20:25
  • that access token will only change if you uninstall your app and reauth, from my understanding. I cache mine in a database, and update anytime i re-auth for tests. I have never really debugged the tokens to see. – ShawnDaGeek Aug 07 '12 at 20:27
  • 1
    Im getting an oauth exception with that saying that the user hasnt authorised this action. – Mrk Fldig Aug 07 '12 at 20:47
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15029/discussion-between-marc-fielding-and-shawn-e-carter) – Mrk Fldig Aug 07 '12 at 20:50
  • Yeah I don't get that page in the graph explorer, i'd imagine its because I didnt create it but I am an admin. – Mrk Fldig Aug 07 '12 at 21:06
0

Ok where I had thought we'd fixed it we haven't the post only works when i'm actually logged into the app itself.

I'm using Shawn's code with all the permissions mentioned with the access token from "me/accounts/" for the AmazingJobs Community page.

I've definitely granted manage_pages, publish stream and offline_acess to my own facebook account i then used the graph api explorer to find the access token within accounts and cut and pasted it into my code. Any ideas?

Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • 1
    Please merge your answers and delete one. Make and accept a definitive answer if you're not going to accept one which is already present. Answers should not be used to document your trial and error process, or further questions. Under each answer and question there's an edit link :) – Ben Swinburne Aug 09 '12 at 14:07