FACEBOOK SDK 4 - 自动发布到页面

时间:2014-11-07 15:40:57

标签: facebook post sdk

直到昨天我才尝试制作一个在Facebook页面上自动发布的剧本。

1 - 我想获得"用户令牌应用程序"谁每1小时过期。我试过curl,file_get_content ....

使用新的SDK你只有3个选择Session by redirect,canvas或javascript ...

使用"令牌应用"

查看我的上一次尝试
    <?php
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );

require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookJavaScriptLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookServerException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/FacebookPermissionException.php' );

use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;

use Facebook\FacebookSignedRequestFromInputHelper;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookJavaScriptLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookServerException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\FacebookPermissionException;

session_start();


class RedditFacebook
{
    protected static $app_id;
    protected static $app_secret;
    protected static $page_id;
    protected static $redirect_uri;
    protected static $app_token;
    protected static $redirect_url;

    function __construct() 
    {
        $this->app_id           = "68183546459998";
        $this->app_secret       = "9270dbghfgh45654c10a799be9f35497";
        $this->page_id          = "doyouwatch";
        $this->redirect_uri     = "http://dojkjhkch";
        $this->redirect_url     = "http://doyhjkhjtch/librarihjkes/redhkjdit-facebook.php";
        $this->app_token        = "fghfghfghfgh654|5fgh4d6g4dfg";

        $this->shareToFacebook();
    }

    private function getShortToken($session)
    {

    }

    private function shareToFacebook()
    {   
        FacebookSession::setDefaultApplication('681837726456456455242938','9270dbcertbfe2erterttcadc9c10a799be9f35497');

        $session = new FacebookSession($this->app_token);

        if($session) {
            try {
                $response = (new FacebookRequest(
                    $session, 'POST', '/'.$this->page_id.'/feed', array(
                        'message'       => "message",
                        'link'          => "http://doyou.watch",
                        'picture'       => "http://doyou.watch"
                    )
                ))->execute()->getGraphObject();
                echo "Posted with id: " . $response->getProperty('id');
            } catch(FacebookRequestException $e) {
                echo "Exception occured, code: " . $e->getCode();
                echo " with message: " . $e->getMessage();
            }
        } else {
            echo "No Session available!";
        }

        echo '<pre>',print_r($session, 1),'</pre>';


        /*
        if($session) 
        {
            try 
            {
                //$post_title = "Do You Watch - ".$post_title."";

                $page_post = (new FacebookRequest( $session, 'POST', '/'. $this->page_id .'/feed', array(

                'access_token' => $this->app_token,

                //title caption
                'name' => "caption_title",

                'link' => "http://doyou.watch",

                //message caption
                'caption' => "caption_description",

                //title first message
                'message' => "post_title"

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

              //error_log("New post facebook send: ".$link."", 0);

            }
            catch(FacebookRequestException $e) 
            {
                error_log("Exception occured, code:". $e->getCode()."", 0);
                error_log("with message: ". $e->getMessage()."", 0);
            }
        } 
        else 
        {
            error_log("No Session available!", 0);
        }   
        */      
    }



}

$obj = new RedditFacebook();

错误:发生异常,代码:200,带有消息:(#200)用户未授权应用程序执行此操作

2 个答案:

答案 0 :(得分:0)

好的,这段代码正常运行!但当我做CRON JOB当然需要自动化。

<?php
session_start();

require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php');
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookSignedRequestFromInputHelper.php');
require_once( 'Facebook/FacebookCanvasLoginHelper.php');
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookPermissionException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php');
require_once( 'Facebook/GraphSessionInfo.php' );

use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookSignedRequestFromInputHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookPermissionException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;


class RedditFacebook
{
    protected static $app_id;
    protected static $app_secret;
    protected static $page_id;
    protected static $page_canvas;

    function __construct() 
    {
        $this->app_id           = "681546465462938";
        $this->app_secret       = "9270dbcbfe2cadc9c1464564565497";
        $this->page_id          = "ghjghjghj";
        $this->page_canvas      = "https://apps.facebook.com/ghjghj";
    }

    public function shareToFacebook($post_title, $caption_title, $caption_description, $link, $category)
    {   
        FacebookSession::setDefaultApplication($this->app_id, $this->app_secret);
        $helper = new FacebookCanvasLoginHelper();

        try 
        {
            $session = $helper->getSession();
        } 
        catch (FacebookRequestException $ex) 
        {
            echo $ex->getMessage();
        } 
        catch (\Exception $ex) 
        {
            echo $ex->getMessage();
        }
        if ($session) 
        {
            try 
            {
                $accessToken = (new FacebookRequest( $session, 'GET', '/' . $this->page_id,  array( 'fields' => 'access_token' ) ))
                ->execute()->getGraphObject()->asArray();

                // save access token in variable for later use  
                $accessToken = $accessToken['access_token'];    

                $session = new FacebookSession($accessToken);


                $response = (new FacebookRequest($session, 'POST', '/oauth/access_token', array(
                'grant_type' => 'fb_exchange_token',
                'client_id' => $this->app_id,
                'client_secret' => $this->app_secret,
                'fb_exchange_token' => $session->getToken())))->execute()->getGraphObject()->asArray();

                $LongTermUserAccessToken = $response['access_token'];

                $session = new FacebookSession($LongTermUserAccessToken);


                $post_title = "Do You Watch - ".$post_title."#".$category."";

                $page_post = (new FacebookRequest( $session, 'POST', '/'. $this->page_id .'/feed', array(

                'access_token' => $accessToken,

                //title caption
                'name' => $caption_title,

                'link' => $link,

                //message caption
                'caption' => $caption_description,

                //title first message
                'message' => $post_title

              ) ))->execute()->getGraphObject()->asArray();
            } 
            catch(FacebookRequestException $e) 
            {
                echo $e->getMessage();
            }
        } 
        else 
        {
            $helper = new FacebookRedirectLoginHelper($this->page_canvas);
            $auth_url = $helper->getLoginUrl(array('publish_actions', 'email'));
            echo "<script>window.top.location.href='".$auth_url."'</script>";
        }   
    }
}

//$obj = new RedditFacebook();

答案 1 :(得分:-1)

private function ShareToFacebook($post_title, $caption_title, $caption_description, $link)
{
    FacebookSession::setDefaultApplication('6818378978942938','9270dbdfgdfgfdgcbfe2cadc9c10a799be9f35497');
    //$session = new FacebookSession('fdgdgdfggT4DFksHSv2mlKiOpzvEVzHkftdH7kcuAH5C0uxXCk9ZAzwsZCdAQlEW9XJIiJfg9OZAORNctfOeFp4vjuMAdZB34uGkU5UinWrHTp4LbPAq4KRAeZBqFh81fmLH7blqXNRNZC6hQ6ZCNpymqkB2AkYC5Df4Uh0ZD');

    $page_id = "doyouwatch";

    $access_token = (new FacebookRequest( $session, 'GET', '/' . $page_id,  array( 'fields' => 'access_token' ) ))
    ->execute()->getGraphObject()->asArray();

    // save access token in variable for later use  
    $access_token = $access_token['access_token'];  

    $session = new FacebookSession($access_token);

    $response = (new FacebookRequest($session, 'POST', '/oauth/access_token', array(
    'grant_type' => 'fb_exchange_token',
    'client_id' => '681837725d64g56dfg5242938',
    'client_secret' => '9270dbcbfe2cadcdfg9c10a799be9f35497',
    'fb_exchange_token' => $accessToken)))->execute()->getGraphObject()->asArray();


    $LongTermUserAccessToken=$response['access_token'];


    $session = new FacebookSession($LongTermUserAccessToken);


    $post_title = "Do You Watch - ".$post_title."";

    $page_post = (new FacebookRequest( $session, 'POST', '/'. $page_id .'/feed', array(

    'access_token' => $access_token,

    //title caption
    'name' => $caption_title,

    'link' => $link,

    //message caption
    'caption' => $caption_description,

    //title first message
    'message' => $post_title

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

}