Reputation: 599
I have a web app that allows people to register directly or by using their facebook credentials through the facebook registration plugin. I want to be able to duplicate information posted on the website to my website's facebook fan page. So far if I have a user that is not logged into facebook, it will post to my Page's wall no problem showing that it came from my website app. But if the person is logged into facebook, but has not granted my app the publish_stream permission I get a permissions error. Is there anyway to simply force the post to come from my website app if I don't have the proper permission with the user? I would be happy if every post on my facebook page came from the app and never from the user, but I simply can't find any documentation showing how this is done or if it even can be done.
EDIT
Here is the code I am using that will allow me to post to my own wall if the person is not logged into facebook. I have already authorized the app to post.
$attachment = array('message' => $notes,
'name' => 'Backcountryride.com',
'caption' => "Community Driven Transportation",
'link' => 'http://backcountryride.com',
'description' => $description,
'picture' => 'http://backcountryride.com/images/vanlogo.jpg'
);
$result = $facebook->api('/backcountryride/feed/','post',$attachment);
EDIT2
Here is the code I use to instantiate the $facebook object:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
Thank you!
Upvotes: 0
Views: 6217
Reputation: 599
THIS NO LONGER WORKS. GO HERE, https://stackoverflow.com/a/11102147/704647
So with some more research, I was able to cobbled together a solution to this problem. In order to do it, I needed to use a combination of the graph API and the legacy REST api. After granting permission to my app to post to my wall, I used the following code to post the entries on my website by my users to my wall.
$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "XXXXXXXXXXXXXX";
$client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$token = curl_exec($curl);
curl_close($curl);
$message = rawurlencode($description.'. Notes:'.$notes);
$url = "https://api.facebook.com/method/stream.publish";
$postString = "message=$message&uid=XXXXXXXXXXXXX&$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
Few things worth noting. 1. The uid in the rest api $postString is the uid for your facebook PAGE. You do not need a target_id because when the uid is of a page it can only publish to itself. 2. Your $message must be rawurlencoded because that is the form accepted by the rest api. 3. This will post to your page as your APP, not as the user who made the post to your website. To do that you would need to have the publish_stream permission from all of your users. Or use the ui dialog to get it with each post. Both of those seem like overkill if all you want to do is post to your OWN page wall.
Since I have seen variations of this question unanswered on stackoverflow in numerous places (I posted this answer on one) I figured it was worth posting this answer here.
If you are unsure as to how to grant your app permission to post to your facebook page wall, here are the two step you need to take. I got this from this post
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
Hope this helps somebody.
Upvotes: 2
Reputation: 599
My former solution no longer works but this does, https://stackoverflow.com/a/11102147/704647
Upvotes: 0
Reputation: 12721
What you are looking for is an "app login". You can get a session for your app instead of the user. Not easy to find, I stumbled across it a few weeks ago. Then you just use the functions you normally use, but with your "app" session instead of a "user" session.
https://developers.facebook.com/docs/authentication/#app-login
Upvotes: 3