views:

2161

answers:

8

I'm able to update the status on my PROFILE wall using this code:

require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook('APP API KEY','APP SECRET KEY');
$user_id = 'MY USER ID';
$facebook->api_client->users_setStatus('This is a new status');

...after authorizing using this address: http://facebook.com/authorize.php?api_key=MYAPPAPIKEY&v=1.0&ext_perm=publish_stream

This code, however, does not work to update the status on my Facebook PAGE Wall. Are there additional parameters that I can add to the authorize.php url to specify authorizing the PAGE and not just my profile?

Or, are there better ways to post updates to Fan Page Walls?

Thanks!

+7  A: 

I solved the problem by consulting the Facebook desktop application documentation (even though this is a web application)... Link: http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications

I first had to authorize offline access with this url (replacing 'MYAPIKEY'): http://www.facebook.com/login.php?api_key=MYAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access

Then, I needed to grant 'publish_stream' permissions to the PAGE with this url (replacing 'MYAPIKEY' and 'THEPAGEID'): http://www.facebook.com/connect/prompt_permissions.php?api_key=MYAPIKEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=popup&ext_perm=publish_stream&enable_profile_selector=1&profile_selector_ids=THEPAGEID

I could then use the following code to publish to the Fan Page wall:

require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook(MYAPIKEY, MYAPISECRET);
try{
    $facebook->api_client->stream_publish('INSERT_STATUS_HERE',null,null,null,'THEPAGEID');
}catch(Exception $o ){
    print_r($o);
}
thechrisvoth
My god, you're awesome. I've been burning my brain out trying to figure out how to grant the page permissions... You're my hero
Dominic Tancredi
I tried working on this but i get FATAL error (Fatal error: Call to a member function stream_publish() on a non-object in ...) . I have given offline authority and publish_stream permissions. I am using the same code.
noobcode
A: 

woah, this sounds sooooooooo legit. pleease pleease PLEASE tell me how to do this? i would appreciate this so much. anything telling me how to do what you just told me would be amazing! a step by step turorial perhaps? im begging you

Cody Brooks
A: 

Greaaat Thanks a lot

Tida
A: 

Thank you so much! It works! (I don't know how to add a comment to the answer itself)

KOHb
A: 

Any idea how (if) this works with the Graph API? I can't seem to manage it. Permissions are all set as above, confirmed in the Facebook UI. But I get the error: "(#200) The user hasn't authorized the application to perform this action" when attempting to update the status.

try { $me = $facebook->api('//feed', 'post', array('message'=> 'new message...', 'cb' => '')); } catch (FacebookApiException $e) { echo var_dump($e); }

Brian
A: 

awesome!

would also like to know how to do this with the graph api

Kevin
A: 

Based on the above, i tried out a couple of querystring parameters on the graph API authorize URL, and it appears this works:

https://graph.facebook.com/oauth/authorize?client_id=[APP_ID]&redirect_uri=[REDIRECT_URL]&scope=publish_stream&enable_profile_selector=1&profile_selector_ids=[PAGE_IDS]

EDIT: Never mind, the above displays all the UI correctly, but still get the "(#200) The user hasn't authorized the application to perform this action" error --- it's clear it doesn't work because the access token contains my USER id but not the page's ID.

Salman Ansari
A: 

@thechrisvoth This is great, is spent one weekend to finish Wall Publishing on a Facebook Page PHP Script, now it works thanks to you. I combine the 2 tutorials : This http://blog.theunical.com/facebook-integration/publish-on-facebook-fan-page-as-fan-user-using-php/, and yours.

Ion