简体   繁体   中英

posting in group on behalf of user using facebook graph API

I am trying to post on behalf of user. I have used tutorial given on this page: http://25labs.com/updated-post-to-multiple-facebook-pages-or-groups-efficiently-v2-0/ .

I could successfully perform authentication but could not post on behalf.

Here is the source code : https://github.com/karimkhanp/fbPostOnBehalf

Testing can be done here: http://ec2-54-186-110-98.us-west-2.compute.amazonaws.com/fb/

Does any one experienced this?

I'm not familiar with the batch process that the tutorial is using but below is a code sample that posts to a Facebook group

<?php 
# same this file as
#     test.php

include_once "src/facebook.php";
$config = array(
    'appId' => "YOURAPPID",
    'secret' => "YOURAPPSECRET",
    'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);

class PostToFacebook 
{
    private $facebook;
    private $pages;


    public function initialise($config){
        $this->name = "Facebook";

        // current necessary configs to set
        // $config = array(
        //  'appId' => FB_APP_ID,
        //  'secret' => FB_APP_SECRET,
        //  'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
        // );

        $this->facebook = new Facebook($config);
        try{
            // if user removes app authorization
            $this->hasAccess = $this->has_permissions();
            if($this->hasAccess){
                $this->groups = $this->getGroupData();
            }
        }
        catch(Exception $err){

        }
    }
    public function postMessageToGroup($message, $groupid){
        $messageResponse = array(
            'STATUS' => 0
        );

        $fbMessageObj = array(
                "message" => strip_tags($message), 
        );
        try
        {
            $user_page_post = $this->facebook->api("/$groupid/feed", 'POST', $fbMessageObj);
            if($user_page_post && !empty($user_page_post['id'])){
                $messageResponse['STATUS'] = 200;
                $messageData = array(
                    'id' => $user_page_post['id'],
                    'link' => 'http://facebook.com/' . $user_page_post['id'],
                );
                $messageResponse['data'] = $messageData;                    
            }
            else{
                $messageResponse['STATUS'] = 302;
            }
        }
        catch(Exception $err){
            $messageResponse['STATUS'] = 500;
            $messageResponse['data'] = array($err);
        }

        return $messageResponse;
    }

    // TODO: should read a template somewhere
    function show_login() {
        $login_url = $this->facebook->getLoginUrl( array( 'scope' => implode(",",$this->permissions()) ));
        return '<a href="' . $login_url . '">Login to Facebook and Grant Necessary Permissions</a>';
    }
    // TODO: should read a template somewhere
    public function toString()
    {
        if($this->hasAccess){
            if($this->groups){
                $msg = "";
                $msg .= '<select name="group_id"><option value=""></option>';
                foreach($this->groups as $group) {
                    $msg .= '<option value="' .
                              '' . urlencode($group['id']) .
                              '">' .
                              $group['name'] .
                              '</option>' .
                              '';
                }
                $msg .= '</select>';
                return $msg;
            }
            else
                return "No Groups";
        }
        else{
            return $this->show_login();
        }
    }

    function getGroupData(){
        $raw = $this->facebook->api('/me/groups', 'GET');
        $data = array();
        if (null != $raw && array_key_exists('data', $raw)) 
            return $raw['data'];
        return null;
    }

    // check if current instance has access to facebook
    function has_permissions() {
        $user_id = @$this->facebook->getUser();
        #print_r($user_id);
        if($user_id == null) return false;
        $permissions = $this->facebook->api("/me/permissions");
        foreach($this->permissions() as $perm){
            if( !array_key_exists($perm, $permissions['data'][0]) ) {   
                return false;
            }
        }
        return true;
    }

    // permissins needed to post
    function permissions(){
        return array('manage_pages', 'user_groups');
    }
}



$fb = new PostToFacebook();
$fb->initialise($config);
if(!$fb->has_permissions())
{
    echo $fb->show_login();
}
else{
    ?>
    <form method="post" action="test.php">
    <textarea name='message'></textarea>
    <?php echo $fb->toString(); ?>
    <input type='submit'>
    </form>
    <?php
}
if(!empty($_POST)){
    $response = $fb->postMessageToGroup($_POST['message'], $_POST['group_id']);
    if($response['STATUS'] == 200)
        print_r("<a href='" . $response['data']['link'] . "'>" . $response['data']['id'] ."</a>");
    else
    {
        echo "ERROR!";
        print_r($response);
    }
}
?>

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