Reputation: 49
I am implementing an application to share a jpg file to user's Facebook account. I am using Facebook PHP sdk-v4-5.0. The phenomena is that the function can successfully upload the photos to my Facebook profile, but it doesn't work for other people. Can someone give me advices? The following is my code. It is developed in Codeigniter framework and named as share_fb.php. The way that Codeigniter to generate url is https://www.example.com/share_fb/post_yoto_with_comment (an example, not a valid link)
<?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();
}
}
}
}
Upvotes: 0
Views: 141
Reputation: 74014
You need to send publish_actions
in for approval before everyone else can use it. Login Review is the keyword: https://developers.facebook.com/docs/facebook-login/review
Without approval, most permissions only work for users with a role in the App.
Upvotes: 2