How to upload photo to page with Graph Api v2.2 - php

I used to create several apps before, but now with the v4 I can't make a simple photo upload to work :( I tried with the 'url' and with the 'source' parameter as well, but nothing works. I want to upload a photo to my own Facebook Page with some caption. I got the access token, the Access Token Debugger says, that it never expires and it has public_profile, read_stream, read_insights, manage_pages, publish_actions, user_photos permissions.
This my code right now:
$accessToken = "MY-ACCESS-TOKEN";
$session = new FacebookSession($accessToken);
$myfile = "file.png"; //created and saved via PHP GD functions
if ( isset( $session ) ) {
try {
$message = "This is my message";
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageid.'/photos',
array (
'source' => realpath("/path/to/" . $myfile),
'message' => $message,
)
);
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
} catch (\Exception $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
} else {
// show login url
echo 'Login';
}
Right now I get this error:
mod_fcgid: stderr: PHP Fatal error: Uncaught exception 'Facebook\\FacebookAuthorizationException' with message '(#324) Requires upload file' in /data/domains/
What am I doing wrong? Can someone help me, please?
Thank you very much!

Related

Get bigger problem size of Facebook profile picture PHP SDK

After successfully signing up, i'm getting users Facebook pictures in this way
/*
* Get user profile picture
*/
try {
// Returns a `FacebookFacebookResponse` object
$response = $fb->get(
'/' .$fbUserProfile['id'] . '/picture',
$_SESSION['facebook_access_token']
);
} catch(FacebookExceptionsFacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$avatar = $response->getHeaders()['Location'];
$this->model_account_customer->addCustomerAvatar($avatar, $this->customer->getId());
So far so good, but the pictures size is 50x50 :( . I read all similiar questions here, but none of them helped me. Also, i've tried to add type=large , i've changed the $response array like that
$response = $fb->get(
'/' .$fbUserProfile['id'] . '/picture',
$_SESSION['facebook_access_token'],
array(
'type' => 'large'
)
);
Still not working. Any ideas would be appreciated :)

Exception occured, code: 200 with message: (#200) Permissions error

I have a problem with FB PHP SDK.
Here is my code:
require_once("autoload.php"); // set the right path
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
$APP_ID = 'APP_ID';
$APP_SECRET = 'APPSECRET';
$TOKEN = "TOKEN";
$ID = "PAGE_ID"; // your id or facebook page id
FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET);
$session = new FacebookSession($TOKEN);
$params = array(
"message" => "Xoşbəxt olmamaq haqsızlıq deyilmi özümüzə qarşı?!"
);
if ($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/' . $ID . '/feed', $params
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
But when i try to run this code it returns an error:
Exception occured, code: 200 with message: (#200) Permissions error
in some forums i read that may be i need to approve premissons/ but when i submited premissions to FB they answered that if i am using it only for my purpose (as app admin) premisson approval is not requred.
What can be problem?
Debug your Access Token: https://developers.facebook.com/tools/debug/
Make sure it includes the manage_pages and publish_pages permission. And make sure you are using a Page Token. More information about the different Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Error when changing a post privacy with the Facebook PHP SDK

What I'm doing is creating a post with the Facebook PHP SDK(v4)(and the app is set to use the v2.3 API) with the privacy settings of value to SELF, then later on changing the value to CUSTOM, and allow to the ids of the friends that have accepted the app.
Until recently everything worked fine, and now out of the blue I'm getting this error(nothing changed on our part): "(#100) Object does not support message editing", which I couldn't locate in their documentation. Has anybody encountered this situation and this error?
Here is the code I'm using (like I said everything worked fine until recently)(if you need more details please let me know)
$fb_token = 'FB_TOKEN';
$FB_session = new FacebookSession($fb_token);
try {
$FB_session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
//echo $ex->getMessage();
throw new RestException(501,'FB error: '. $ex->getMessage());
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
//echo $ex->getMessage();
throw new RestException(501,'FB error: '. $ex->getMessage());
}
//make posting request to FB
if($FB_session) {
try {
$fb_array = array(
'privacy' => array(
'value' => 'CUSTOM',
'allow' => 'USER_ID'
)
);
$fb_req = new FacebookRequest(
$FB_session, 'POST', "/POST_ID", $fb_array
);
$response = $fb_req->execute()->getGraphObject();
//echo "Posted with id: " . $response->getProperty('id');
$fb_post_id = $response->getProperty('id');
} catch(FacebookRequestException $e) {
//echo "Exception occured, code: " . $e->getCode();
//echo " with message: " . $e->getMessage();
throw new RestException(501,'FB error: '. $e->getCode() .'-'. $e->getMessage());
}
}

Facebook user Graph does not return Email PHP

I am new to the whole Facebook API.
I am creating a login page for users, and the idea is that they login with Facebook.
At the top of login page I have the following code:
$fb = new Facebook\Facebook([
'app_id' => "$fb_appid",
'app_secret' => "$fb_appsecret",
'default_graph_version' => 'v2.2',
]);
Then a little lower I have the login button:
if(isset($fb)){
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // optional
$fb_login_url = $helper->getLoginUrl("$server_url/public/facebook/login-callback", $permissions);
} else $fb_login_url = "";
if(strlen($fb_login_url) >= 1) echo "<a href='$fb_login_url'><i class='fa fa-2x fa-facebook-square'></i></a>";
else echo "<a href='#' disabled='disabled'><i class='fa fa-2x fa-facebook-square'></i></a>";
The code works up until there, if I click the button, I go through to the Facebook portal, click the allow for the permissions and whatever, then it redirects me to login-callback.
The code on login-callback is:
if(isset($fb)){
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
}
if (strlen($err_message) <= 0 && isset($accessToken)) {
$_SESSION['facebook_access_token'] = (string) $accessToken;
$fb->setDefaultAccessToken("$accessToken");
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
// $username = $userNode->getName();
// $firstname = $userNode->getFirstName();
// $lastname = $userNode->getLastName();
print_r($userNode);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
}
}
}
The $userNode looks like this:
Facebook\GraphNodes\GraphUser Object
(
[items:protected] => Array
(
[name] => Jacques Koekemoer
[id] => xxxxxxxxxxxxxxxxxx
)
)
I have set the permissions to allow for the email, and if I am not mistaken the public profile is sent automatically.
I have also checked that the button on the login page does have "&scope=email". Below is the code that I have right on the page right now in the login button:
https://www.facebook.com/v2.2/dialog/oauth?client_id=xxxxxxxx&state=xxxxxxxx&response_type=code&sdk=php-sdk-5.0.0&redirect_uri=http%3A%2F%2Fxxxxxxx.xxxxxxxxxxxx.co.za%2Fpublic%2Ffacebook%2Flogin-callback&scope=email
I replaced the client_id, state and domain name because I don't want that information available publicly as I don't know what people can do with it.
Let me know if it is needed to solve the problem.
I used the Facebook guide here to setup and download the SDK.
I solved the problem.
You need to request specific fields from Facebook in the get function.
$response = $fb->get('/me?fields=id,name,email');
This can be found here.
A full list of fields that you can query can be found here.
Example
if (isset($fb)) {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
}
if (strlen($err_message) <= 0 && isset($accessToken)) {
$_SESSION['facebook_access_token'] = (string)$accessToken;
$fb->setDefaultAccessToken("$accessToken");
try {
// this here is where you specify the fields
$response = $fb->get('/me?fields=id,name,email');
$userNode = $response->getGraphUser();
/* handle the result */
} catch (Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
}
}
}
One of the my friend was trying to the issue from last 5 days. huummm very irritating. which got love in 2 min...
Assumption: using PHP for O2Authentication
Solution:
$loginURL = $fb->getLoginUrl({your redirect URL},['email']);
And job is done.
Thanks & Regards
Jaiswar Vipin Kumar R.
Check you app version if it is version 2.2 or lower, it will work fine , it is not than it will not return. for that you have to request api with scope for email access.

facebook api file upload getting 324 error

I'm using php facebook api 4.0. I'm using below code to post photo on my timeline.
FacebookSession::setDefaultApplication($app_id, $app_sc);
$helper = new FacebookRedirectLoginHelper($url_page);
$loginUrl = $helper->getLoginUrl();
$session = new FacebookSession($access_token);
FacebookSession::enableAppSecretProof(false);
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/photos', array(
'source' => '#' . realpath('1.jpg'),
'message' => 'message'
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '#/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
But I'm getting Exception occured, code: 324 with message: (#324) Requires upload file error. I think one thing is that, fileupload support should be set to on. But I don't know from where should I turn it on.

Categories