我正在尝试设置我的 WP 网站,以便在发布帖子时将其发送到 Facebook 页面。不幸的是,我无法让它发挥作用。下面的代码被包括在内,但似乎什么也没做(可能 FB 正在发回一个错误,但 WP 没有显示它)。
当我尝试访问我希望发布的页面的提要(下)时,我被告知我没有正确的权限(也在下)
/** The error I recieve when visiting the above page */
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
到目前为止,我已经完成了这些设置 -
- 访问Graph API Explorer
- 从下拉菜单中选择我希望发布到我的主页的应用程序
- 单击“获取访问令牌”
- 选择了“manage_pages”权限
- 重新登录,授予应用访问我的页面的权限
- 创建了以下脚本,包含已生成的访问令牌。
请有人帮忙,因为我不知道从这里去哪里。谢谢。
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);