Facebook PHP SDK Graph API post to page not functioning properly

两盒软妹~` 提交于 2020-02-03 18:51:29

问题


I'm trying to add a feature to my app that will post a status to a admin user's page timeline with the same detail as if they posted on Facebook.com. The main feature I am focusing on is the link sharing and thumbnail images, like when you paste a link into your status and it auto-detects a thumbnail image, gives you a nice link, description, etc. I have tireless read through other forums and the Graph API documents, and I keep running into problems with the post showing as the admin user, rather than the page. Here is my code:

$facebook = new Facebook(array(
  'appId'  => $appID,
  'secret' => $appSecret,
));

$loginUrl = $facebook->getLoginUrl(array(
    "scope" => 'publish_stream, read_insights, manage_pages, photo_upload, video_upload, create_note, manage_notifications'
));

$access_token   = $facebook->getAccessToken();

$fbpost = array();

$fbpost['access_token'] = $access_token;
$fbpost['message']      = $message;
$fbpost['link']         = $link;
$fbpost['description']  = $description;
$fbpost['caption']      = $caption;
$fbpost['picture']      = $fbimg;

$status = $facebook->api('/'.$pageID.'/feed', 'POST', $fbpost);
var_dump($status);

When I only post the $fbpost['message'] it correctly posts the status as the page, but when I add ANYTHING else it shows the post as the authenticated admin user instead of the page. Very frustrating. Any ideas?


回答1:


I keep running into problems with the post showing as the admin user, rather than the page.

Then get a page access token, not a user access token for the admin user …




回答2:


Thanks to CBroe for providing a link to the Facebook docs on the page/app access token. After checking that out, I came up with this PHP (since there is no good documentation in the PHP SDK for getting a page access token):

$user_token     = $facebook->getAccessToken();
$accounts       = $facebook->api('/me/accounts?access_token='.$user_token);
$account_token  = 0;
foreach ($accounts['data'] as $account) {
    if ($account['id'] == $_SESSION['facebook']) {
        $account_token = $account['access_token'];
    }
}

if ($account_token) {
    /// your page token code
} else {
    echo 'You must be an admin on this page!';
}


来源:https://stackoverflow.com/questions/11054915/facebook-php-sdk-graph-api-post-to-page-not-functioning-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!