简体   繁体   中英

How can I post status to facebook page with page name rather than user name?

How can I post a status message to a page (not a user profile) using the PHP SDK and graph API while having the status message appear to be posted by the page ID rather than a user ID? My app can successfully post a status update to the page wall but it appears to be a wall post from the admin's username rather than a status update from the page name as it would if posted locally on Facebook. In other words if the page name is "Cool Stuff" and the user name is "Joe Smith", I want to post a status update on the page from "Cool Stuff" but the updates my app posts appear from "Joe Smith". I'm already explicitly setting the page id and page name in the status array but it doesn't seem to help. Here's my current code:

$post_info = array(
 'access_token' => $facebook->access_token,
 'type' => 'status',
 'message' => 'Hello World!',
 'from' => array('id' => $page_id, 'name' => $page_name)
);
$facebook->api('/' . $page_id . '/feed/', 'post', $post_info);

Any suggestions?

I found the solution to this in the FB API authentication docs here:

https://developers.facebook.com/docs/authentication/

The answer is way down near the bottom of the page. Search for the "page login" section. Basically your app has to query FB for a list of the user's page access tokens. You find the token for the page you want to act as and substitute it for the normal user access token in the status message post. So I added this code to grab the user's page access tokens (your app has to have the manage_pages permission for this to work):

$all_accounts = $facebook->api('/me/accounts');

If you use print_r to dump $all_accounts, you'll see it's an array of page names and access tokens. Grab the token for page you want and replace $facebook->access_token (the user's access token) with the page token in the code I posted in my initial question. It will post the status to the page as the page admin rather than the user.

If you are the admin of that page just simply authenticate and send the status as the page's UserID instead of your UserID. They are 2 different entities.

Doing that will solve your problem :-) Just make sure to manually authenticate your page against your App first :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM