Facebook PHP sdk-v4-5.0无法上传除开发者之外的照片

时间:2015-07-21 09:44:33

标签: php facebook upload facebook-php-sdk

我正在实施一个应用程序来共享一个jpg文件到用户的Facebook帐户。我正在使用Facebook PHP sdk-v4-5.0。现象是该功能可以成功将照片上传到我的Facebook个人资料,但它不适用于其他人。有人可以给我建议吗?以下是我的代码。它是在Codeigniter框架中开发的,命名为 share_fb.php 。 Codeigniter生成网址的方式是 https://www.example.com/share_fb/post_yoto_with_comment (例如,不是有效链接)

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    use Facebook\FacebookRequest;
    require_once('/home/ubuntu/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php');

    class Share_fb extends CI_Controller 
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->library('session');

        }



        public function post_yoto_with_comment()
        {
            session_start();
            $app_id = "{app_id}";
            $app_secret = "{app_secret}";
            $my_url = base_url().'share_fb/post_yoto_with_comment';
            $fb = new Facebook\Facebook([
                'app_id' => $app_id,
                'app_secret' => $app_secret,
                'default_graph_version' => 'v2.2'
            ]);

            $helper = $fb->getRedirectLoginHelper();

            if( $this->input->get_post('code') )
            { 
                $code = $this->input->get_post('code') ; 
            }
            if(empty($code)) 
            {
                $permissions = ['email', 'publish_actions']; // optional
                $loginUrl = $helper->getLoginUrl($my_url, $permissions);
                header('location:'.$loginUrl );
                return;
            } 

            $helper = $fb->getRedirectLoginHelper();
            $accessToken = '';
            try 
            {
                $accessToken = $helper->getAccessToken();

            } 
            catch(Facebook\Exceptions\FacebookResponseException $e) 
            {
                echo 'Graph returned an error: ' . $e->getMessage();
                exit;
            } 
            catch(Facebook\Exceptions\FacebookSDKException $e) 
            {
                echo 'Facebook SDK returned an error: ' . $e->getMessage();
                exit;
            }

            if(isset($accessToken))
            {
                $_SESSION['facebook_access_token'] = (string) $accessToken;
                $this->session->set_userdata('access_token', (string) $accessToken);
                $fb->setDefaultAccessToken($accessToken);
                $image = "/var/www/tmp/yoshiki.jpg";//.$this->session->userdata('ssfilename');
                try
                {
                    $data = [
                            'source' =>  '@'.$image,
                            'message' => "rrrr",
                        ];
                    $response = $fb->post('/me/photos', $data, $accessToken);
                } 
                catch(FacebookRequestException $e) 
                {

                    echo "Exception occured, code: " . $e->getCode();
                    echo " with message: " . $e->getMessage();

                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

您需要先发送publish_actions进行审批,然后其他人才能使用它。 登录评论是关键字:https://developers.facebook.com/docs/facebook-login/review

未经批准,大多数权限仅适用于在App中具有角色的用户。