I have the following script and would like to mention for ex. another page, which is involved in the post. I tried severall things, but can´ get it work. Isn´t it possible?
Here is my script:
header("Content-Type: text/html; charset=utf-8");
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => ‚1*************‘,
'app_secret' => ‚*e************,
'default_graph_version' => 'v2.2',
]);
$params["message"] = "Here is the magic - Thanks for your support, #[pageid]";
$params["link"] = "";
$params["picture"] = "";
$params["description"] = ";;
$pageAccessToken ='**************************';
try {
$response = $fb->post('/me/feed', $params, $pageAccessToken);
} 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;
}
$graphNode = $response->getGraphNode();
I tried, using #[pageid] and #[page-id:page-name], but both is not working.
Would be great to get any help.
From my experience, I will say error from facebook is always different based on configuration of your app, page etc. So it is difficult to say without seeing the exact error message. Just change your code a little bit to see error message is details:
try {
$response = $fb->post('/me/feed', $params, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
var_dump($e);
exit;
}
I am sure, you will get error details which will guide you to solve your problem.
Related
What permissions do I need to get in order to use the notifications API? My test app contains two files:
nfytest.php:
<?php
require_once "../vendor/facebook/graph-sdk/src/Facebook/autoload.php";
session_start(); $_SESSION = array();
echo "<html><body>";
try{
$fb = new Facebook\Facebook(['app_id' => 'xxx','app_secret' => 'xxx', 'default_graph_version' => 'v3.2']);
$helper = $fb->getRedirectLoginHelper();
$loginurl = $helper->getLoginUrl("https://www.bkassist.com/dnamatchmakerdev/callback.php", 'email', 'publish_to_groups']);
echo "<a href='$loginurl'>Pleaseclick on this link</a>";
} catch(Facebook\Exceptions\FacebookResponseException $e) {
error_log($e->getMessage());
echo 'Graph returned an error: '. $e->getMessage();
}catch(Facebook\Exceptions\FacebookSDKException$e){
error_log($e->getMessage());
echo 'Facebook SDK returned anerror: ' . $e->getMessage();
}
echo "</body></html>"; return 200;
?>
callback.php:
<?php
require_once "../vendor/facebook/graph-sdk/src/Facebook/autoload.php";
session_start();
ini_set("error_log", "error.log");
echo "<html><body>";
try
{
$fb = new Facebook\Facebook(['app_id' => 'xxx', 'app_secret' => 'xxx', 'default_graph_version' => 'v3.2']);
$helper = $fb->getRedirectLoginHelper();
$at = $helper->getAccessToken();
$fb->post("/2465651860/notifications", ["template" => "#[2465651860] has posted something you said you were interested in", "href " => "index?nfycallback"], $at);
echo "Success. Or at least not failure.";
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
error_log($e->getMessage());
echo 'Graph returned an error: ' . $e->getMessage();
}
catch(Facebook\Exceptions\FacebookSDKException $e)
{
error_log($e->getMessage());
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
echo "</body>/</html>";
return 200;
?>
Running this produces the error: Graph returned an error: Unsupported post request. Object with ID '2465651860' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api/
I have done, but I haven't found enlightenment yet. [Sorry about the formatting of the code -- I swear I tried to make it look ok!]
First, I think you should not paste your app info in the question. So change the password to protect your app.
Then, I have do some tests, Only not give the access_token parameter will return this error. So check the the access token parameter?
BTW, This method must be called with an app access_token. and only can send notifications who has installed your app.
I just copied the following script from facebook
https://developers.facebook.com/docs/php/gettingstarted
I downloaded the main code using composer
<?php
session_start();
require_once '../../Facebook/autoload.php';
print_r($_SESSION);
//if(!isset($_SESSION['user']) || ($_SESSION['user'] < 1) || ($_SESSION['user'] == '')){
$fb = new Facebook\Facebook([
'app_id' => '1444966872674589',
'app_secret' => '54362fa0c423jdui348758ea172537fb',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getCanvasHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}
//}
But above code throws error
Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'Signed request has an invalid signature.'
I have already read this links
Integrating facebook in php giving invalid signed request with oAuth data
and
Integrating facebook php sdk in Facebook Canvas App giving Blank page or oauthData error
But it didn't solve my problem
please help I can't solve the problem
If you are calling your script from your website you can try this:
replace:
$helper = $fb->getCanvasHelper();
with:
$helper = $fb->getRedirectLoginHelper();
also replace:
if (isset($accessToken)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}
with:
$permissions = 'list of facebook permissions';///optional
if (isset($accessToken)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}else{
$loginUrl = $helper->getLoginUrl('url-of-calling-script.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
I am having a weird issue. Every time I try to read a specific video from FB, it tells me:
(#100) type must be one of the following values: tagged, uploaded
Which is odd, as the access token in this case IS ME, and I uploaded the video, so I SHOULD be able to read the data.
when I hit "/me/videos/uploaded" it returns a list of my videos, with the correct id's (I thought maybe I was using the wrong value).
here is the quick and dirty I am using to test
public function fbtestAction() {
$code = $this->params()->fromQuery('code');
$state = $this->params()->fromQuery('state');
$fb = new \Facebook\Facebook([
'app_id' => [add_id],
'app_secret' => [app_secret],
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if(!$code) {
$permissions = ['user_videos'];
$loginUrl = $helper->getLoginUrl('http://test.local/fbtest', $permissions);
die("<a href='$loginUrl'>clickity</a>");
} else {
try {
$accessToken = $helper->getAccessToken();
$fb->setDefaultAccessToken($accessToken);
} 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;
} finally {
echo '<pre>';
//var_dump($fb->get('/me'));
var_dump($fb->get('/me/videos/uploaded'));
//var_dump($fb->get('/videos/[video_id]'));
echo '</pre>';
die;
}
}
}
`
Reading comprehension for the win. The proper endpoint is "/[video_id]" not "/videos/[video_id]"
I would like to use the Facebook PHP SDK v5 to automatically post to a FAN PAGE. I have the code below which successfully posts to my own wall, but how do I alter the code to post to my fan page? From what I've read, I need to pass in the page id of the fan page?
$params["message"] = 'test';
$params["link"] = 'https://example.com';
$params["picture"] = 'https://example.com/images/logo_hod.jpg';
$params["description"] = 'testtt';
$access_token = $accessToken;
try {
$response = $fb->post('/me/feed', $params, $access_token);
} 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;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
I tried replacing this line:
$response = $fb->post('/me/feed', $params, $access_token);
with this to substitute in the fan page id:
$response = $fb->post('/229107363768165/feed', $params, $access_token);
and got the error:
Graph returned an error: Unsupported post request.
UPDATE: I also made the app "public" in an attempt to get past the "unsupported post request" error. No luck.
i think your $params is not right.
I use this:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$params = [
'link' => 'https://example.com',
'photo' => 'https://example.com/images/logo_hod.jpg',
'message' => 'A test post!',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $params, '{access-token}');
} 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;
}
Post Links Using the Graph API
I assumes that you've already obtained an access token and the access token must have the publish_actions permission for this to work.
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.