I’m trying to set my WP site up so that when I publish a post, it is sent to a Facebook Page. Unfortunatly I cannot get this to work. The code below is being included, but just seems to be doing nothing (presumably FB is sending back an error, but WP is not displaying it).
When I try to visit the feed for the Page I wish to post on (below), I’m told that I do not have the correct permissions (also below)
/** The error I recieve when visiting the above page */
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
I’ve so far completed these setps –
- Visit to the Graph API Explorer
- Select the App that I wish to post to my Page from the dropdown menu
- Clicked ‘Get Access Token’
- Selected the ‘manage_pages’ permission
- Re-signed in, granting permission for the App to access my Pages
- Created the followin script, incorporating the Access Token that has been generated.
Please can someone help, as I have no idea where to go from here. Thanks.
class Publish_To_Facebook{
/**
* The ID of the Page to edit
*
* @var string
* @access private
*/
private $page_id = '163797013684290';
/**
* The Page access token given to the application set up to post to the Page
*
* @var string
* @access private
*/
private $page_access_token = 'my_access_token';
/**
* The back-end service for Page's wall
*
* @var string
* @access private
*/
private $post_url = '';
/**
* Constructor
*/
public function __constructor(){
$this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed';
}
/**
* Manages the POST message to post an update on a page wall
*
* @param array $data
* @return string the back-end response
*/
public function message($data){
/** Grab the $page_access_token */
$data['access_token'] = $this->page_access_token;
/** Initiate CURL */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/** Execute and close CURL */
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
}
$to_post = array(
'message' => 'The status header',
'link' => 'http://theurltopoint.to',
'picture' => 'http://thepicturetoinclude.jpg',
'name' => 'Name of the picture, shown just above it',
'description' => 'Full description explaining whether the header or the picture'
);
$facebook = new Publish_To_Facebook();
$facebook->message($to_post);
I think you should look at this page to fix it. It describes the full process.
Login as a Page
Here is some info from the documentation regarding the Page access token:
Source