I'm learning how to create a web app to manage my group using Facebook PHP SDK v5.0. When I delete my group members, system throw an exception:
Exception 'Facebook\Exceptions\FacebookResponseException' with message
'(#3) Unknown method'.
This is my code
public function removeMemberByMemberId($group_id, $memberid, $fb, $access_token){
try {
$request = $fb->delete('/'.$group_id .'/members', ['members' => "'" . $memberid . "'"], $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;
}
}
Please help me!
Thank you so much!
Related
I have the following PHP code to post in a Facebook Group Feed where the the publisher is admin of. The code is as follows:
$post_array=array ('link'=>$link_share,'message' => $message);
try {
$response = $fb->post(
"/{$group_id}" . "/feed",
$post_array,
"{$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();
But when I execute it I get the error:
Graph returned an error: Unsupported post request. Object with ID 'group_id' 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 read all the documentation, the user has the permissions to publish in the group but I keep getting that error. Any ideas why?
Thanks
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>";
}
My code
try {
$response = $fb->get('/me?fields=id,name,gender,email,picture', $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;
}
$user = $response->getGraphUser();
echo 'Name:'.$user['name']."</br>";
echo 'gender:'.$user['gender']."</br>";
echo 'picture:'.$user['picture']."</br>";
echo 'email:'.$user['email']."</br>";
My output
Name:Siddhu Siddhartha Roy gender:male
picture:{"is_silhouette":false,"url":"https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12295307_890342681035161_8265283125930543183_n.jpg?
oh=f8198fc14435078e584749f2d55dcd8e&oe=5833FDCD"}
email:siddhu.08pc1a0413#gmail.com
My Problem
I'm unable to show profile picture.. when I goto URL
https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12295307_890342681035161_8265283125930543183_n.jpg
it shows that Content not found
Simple: You're getting the data in JSON format. So decode the result.
$json = json_decode($user['picture'], true);
echo $json['url'];
I want to get following local insights of Facebook page using graph API
People Nearby:Hourly
Weekly
Overall
Check-ins
Please see the screen shot.
Image
You can get page insights by using this code and ask for page permissions ('manage_pages','pages_manage_cta') , and get page id first from your likes or managed pages and replace you page id in code with YOUR_PAGE_ID.
use FB php sdk version: facebook-php-sdk-v4-5.0-dev.
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4', // or use v2.5 latest version
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages','pages_manage_cta'];
$redirectUrl = 'http://localhost/fbapp.php';
$loginUrl = $helper->getLoginUrl($redirectUrl, $permissions);
echo 'Log in with Facebook!';
After generating login url implement the code for response handling and getting required data.
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
setcookie('accessToken',$accessToken);
} 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;
}
$fb->setDefaultAccessToken($accessToken);
// Get user groups detail
$requestPageInsights = $fb->request('GET', '/YOUR_PAGE_ID/insights');
//Make a batch request
$batch = ['page-insights' => $requestPageInsights];
try {
$responses = $fb->sendBatchRequest($batch);
} 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;
}
for a better view can use this:
foreach ($responses as $key => $response) {
if ($response->isError()) {
$e = $response->getThrownException();
echo '<p>Error! Facebook SDK Said: ' . $e->getMessage() . "\n\n";
echo '<p>Graph Said: ' . "\n\n";
var_dump($e->getResponse());
} else {
echo "<p>(" . $key . ") HTTP status code: " . $response->getHttpStatusCode() . "<br />\n";
echo "Response: " . $response->getBody() . "</p>\n\n";
echo "<hr />\n\n";
}
}
And for getting other details visit on and use your related params/scope https://developers.facebook.com/docs/graph-api/reference/v2.5/insights
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.