Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook API: How to publish to Page feed while user is offline without offline_access permission

Using Facebook's Graph API, I've been successful at publishing to a user's feed while the user is offline with only the publish_stream permission. I don't need the offline_access permission. I explained how I did this here: Facebook Stream Publish while offline

I haven't been as successful with publishing to a Page while the user is offine. Here's my scenario:

User U is an admin of Page P. P authorizes and installs my app. U authorizes my app and grants me the following extended permissions:

  • publish_stream
  • manage_pages

Once the user is offline, if I try the same technique I use to publish to a user's stream (without offline_access permission) to instead publish to a Page, I get a "The user hasn't authorized the application to perform this action". Here's the technique:

1) fetch my app's access_token

2) use my app's access_token to publish to Page P's feed: POST https://graph.facebook.com/{page_id}/feed

If instead of {page_id} in step 2, I use {user_id}, then it publishes to the user's feed without any problem. However, I'd like to publish to the Page's feed. Is it possible to do this? Or do I need the offline_access permission from the user in order to do this?

Thanks, Johnny

like image 701
Johnny Oshika Avatar asked Mar 06 '11 07:03

Johnny Oshika


1 Answers

Full flow - four page example (easier to edit and understand for an example)

So the config just has the basic app info...

When you load index.php - it redirects to facebook to get authorization for the page.. (probably already have this.. but covering all bases)

Facebook then redirects back to the redirecturl (backfromfb.php)...

Facebook returns the access token as a hash and not a get variable, so this page refreshes with the hash as a variable...

It then loads the PageUpdate.php which handles looping through the app/admin tokens and finds the correct one for the page you want to post to..

Then it builds the post and submits it..

It sounds like you have an understanding of most of this... So hopefully this will help you with that last bit.

config.php

<?php
$config = array(
    'appId' => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET',
    'cookie' => true;
    );

$baseurl = 'http://yoursite.com/';
$returnpage = 'backfromfb.php';

require_once('library/facebook.php');

?>

index.php

<?php require_once('config.php'); ?>

<html><head><title>Redirecting for auth</title></head><body><script type="text/javascript">
            window.location.href = 'https://www.facebook.com/dialog/oauth?client_id=<?php echo $config['appId']; ?>&redirect_uri=<?php echo $baseurl . $returnpage; ?>&scope=manage_pages&response_type=token';
</script></body></html>

backfromfb.php

<?php
require_once('config.php');
// this page just passes the access token from the hash to a GET arg...
if (!isset($_GET['access_token'])) {
?>
<html><head><title>Redirecting</title></head><body><script type="text/javascript">
            accessToken = window.location.hash.substring(1);
            window.location.href = '<?php echo $baseurl . $returnpage; ?>?' + accessToken;
</script></body></html>
<?php
} else {
require_once('PageUpdate.php');
} ?>

PageUpdate.php

<?php
require_once('config.php');

$pageID = "123456 WHatever you page id is";

$AppToken = array(
    'access_token' =>  $_REQUEST['acess_token']
);

$fb = new Facebook($config);

// Load APP page access rights for user via AppToken
$pageAdmin = $fb->api('/me/accounts', 'GET', $AppToken);

// Find the page access token
foreach ($pageAdmin['data'] as $data) {
    if ($data['id'] == $pageID) {
        $pageToken['access_token'] = $data['access_token'];
        continue;
    }
}

// compile the post
$WallPost = array(
    'message' => 'Test post from my app!'
);  // you can also use 'picture', 'link', 'name', 'caption', 'description', 'source'.... 
    //http://developers.facebook.com/docs/reference/api/


// post to wall
$response = $fb->api($pageID . '/feed','POST',$WallPost);

if($response) {

    echo 'Success!';
    echo '<pre>' . $response . '</pre>';

} else echo "failed";


?>
like image 97
CarpeNoctumDC Avatar answered Oct 08 '22 17:10

CarpeNoctumDC