0
votes

Ok so I finally managed to successfully link dynamic news posts on my website with my page's wall as Page (not user) through PHP. Still I'm wondering if my solution is correct, or if it'll work on the long-run. Having said that here's what I did:

  • Created a profile to administrate the page
  • Created the company page
  • Created an app with the domain pointing to my site domain, and website pointing to my website url
  • Disable offline_access deprecation in order to be able to issue offline_access tokens
  • Found out my pageID through http:// graph.facebook.com/PAGE_NAME
  • Went to https:// developers.facebook.com/tools/explorer/APP_ID
  • Paste the pageID in the instead of the userID and clicked submit
  • Then I clicked on get access token and checked manage_pages,publish_stream,offline_access

(when this alone didn't work, I visited https://developers.facebook.com/docs/authentication/ and under "Page Login" I found out that Page Login requires a different type of token...)

require_once('facebook-sdk/facebook.php');
//Required facebook auth vars
$appID = 'APP_ID';
$appSecret = 'APP_SECRET';
$pageID = 'PAGE_ID';
$appOfflineToken = 'TOKEN_RETURNED_IN_PREVIOUS_STEP';
$pageTokenURL = 'https://graph.facebook.com/me/accounts?access_token=';
$access_token = '';

//connect to facebook app
$facebook = new Facebook(array(
    'appId'  => $appID,
    'secret' => $appSecret,
    'cookie' => true
));

//get page managed pages information
$jsonData = file_get_contents($pageTokenURL.$appOfflineToken);
$content = json_decode($jsonData, true);

//filter access_token for desired page using pageID
foreach($content['data'] as $item) {
    if($item['id'] == $pageID){
        $access_token = $item['access_token'];
        break;
    }
}

//format post
$post =  array(
    'access_token' => $access_token,
    'picture' => "http://URL_TO_PICTURE,
    'link' => "http://URL_TO_NEWS_POST",
    'name' => "NEWS_TITLE",
    'description' => 'NEWS_DESCRIPTION'
);

//post content to page wall
$res = $facebook->api('/'.$pageID.'/feed', 'POST', $post);

so my question is ... even though this seems farfetched ... is it correct? well at least it works!

PS: sorry about the links but could only submit 2 in this post ... not enough street cred it seems :p

1
You seems right. Anyway, why are you doing this manually?gremo
well because as it turns out you only need to issue the app offline_access access token once ... I did however update the script to include a fail safe in case for some reason the token dies out that generates a new token (because in the documentation they say offline_access tokens are long lived they just don't say how long). I mainly posted this because most of what I found on google did not cover the idea of having news updates in the website synced up with the page wall... also because I was unsure that this process was correct if it was somehow a "hack" or if there was some other way :PFernando Luís
offline_access will be deprecated in a few weeks. However your existing (never expires) access token will continue to work. Anyway check developers.facebook.com/docs/offline-access-deprecationgremo

1 Answers

-1
votes

You seems right but the only problem it offline_access

i am work in this from two weeks only my problem offline_access i want to how many days or weeks works because when in my project the offline_access work for two hours and is there problem in my code or what ?