我尝试通过网站作为页面管理员发布到我的Facebook页面。但我可以用我的个人账户来做到这一点。
这是我的代码:
private $fb_api;
private $fb_secret;
private $fb_redirect = 'index.php/admin/ajout_actualite';
private $fb_helper;
private $fb_scopes = array(/*'publish_actions', 'email', */'create_event', 'manage_pages', 'publish_stream');
private $fb_session;
$data = $this->config->item('facebook')['key'];
$this->fb_redirect = base_url($this->fb_redirect);
$this->fb_api = $this->config->item('facebook')['key'];
$this->fb_secret = $this->config->item('facebook')['secret'];
FacebookSession::setDefaultApplication($this->config->item('facebook')['key'], $this->config->item('facebook')['secret']);
$this->fb_helper = new FacebookRedirectLoginHelper($this->fb_redirect);
if ( $this->session->userdata('access_token') ) {
$this->fb_session = new FacebookSession( $this->session->userdata('access_token') );
// Validate the access_token to make sure it's still valid
try {
if ( ! $this->fb_session->validate() ) {
$this->fb_session = false;
}
} catch ( Exception $e ) {
// Catch any exceptions
$this->fb_session = false;
}
}
这就是我在网页上发布的内容:
$loginUrl = $this->fb_helper->getLoginUrl($this->fb_scopes);
if($this->input->get('code') != null) {
$s = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=".$this->fb_api."&redirect_uri=".urlencode($this->fb_redirect)."&client_secret=".$this->fb_secret."&code=".$this->input->get('code'));
$s = parse_str($s, $out);//var_dump($s);
$this->session->set_userdata(array("access_token" => $out['access_token']));
}else if($this->session->userdata('access_token') == NULL) {
redirect($loginUrl);
}
if($this->fb_session != null){
$response = (new FacebookRequest(
$this->fb_session, 'POST', '/XXXXXXXX/feed', array(
'name' => 'This is drop da bit',
'caption' => "I love you baby",
'link' => 'http://lesjoiesducode.fr/',
'message' => 'Unicorn 1 - 0 Facebook'
)
)) - >执行() - > getGraphObject() - > asArray(); }
我希望有人可以帮助我:),我已经阅读了4-5个主题,但在以前的sdk中占多数:/
提前感谢您的帮助。
答案 0 :(得分:1)
您正在使用用户访问令牌和/{page-id}/feed
,这就是代表用户发布帖子的原因。
要代表网页本身发帖,您必须使用page access token。要获取页面访问令牌,请先将权限manage_pages
添加到登录范围,然后进行调用 -
/{page-id}?fields=access_token
这将为您提供页面访问令牌。要在/{page-id}/feed
调用中使用此令牌,只需使用其他参数传递其他参数access_token
。