简体   繁体   中英

Auto post on facebook page as page admin with sdk4 (graph api)

I try to post to my facebook page via a website as the page admin. But I just can do that with my personnal account .

This is my code :

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;
        }
    }

And this is how I post on the page :

$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'
                )

))->execute()->getGraphObject()->asArray(); }

I hope someone will can help me :), i've read 4-5 topics but there are in majority on the previous sdk :/

Thanks in advance for your help.

You are using the user access token with the call /{page-id}/feed that's why the post is published on behalf of the user.

To post on behalf of the page itself, you have to use the page access token . To get the page access token, add the permission manage_pages first to the login scope then make the call-

/{page-id}?fields=access_token

this will give you the page access token. To use this token with the /{page-id}/feed call just pass an additional parameter access_token with other parameters.

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