Retrieving email from Facebook GraphObject - php

I am not able to retrieve the email from the Graph Object. I see on my app that I have permission for it. Here is my current code:
FacebookSession::setDefaultApplication('xxx','xx');
$helper = new FacebookRedirectLoginHelper('xxx');
$session = null;
try {
$session = $helper->getSessionFromRedirect();
Core::session('FacebookAuthSession')->sessionObject = $session;
} catch(FacebookRequestException $ex) {
} catch(\Exception $ex) {
}
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();

You can use following code to execute:
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
if($session) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Email: " . $user_profile->getEmail();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
To test results, you can also use explorer: https://developers.facebook.com/tools/explorer

The permissions are just listed as "approved without review" in your App settings, but you still have to authorize a user with them by using the "scope" parameter in the login process: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

Related

Facebook PHP SDK v4 manage_pages permission for non developer account

We are trying to create an app to pull in facebook reviews which requires the manage_page permission. This is not an app which we would like to submit for review as we are only using this for our clients.
We are currently able to get the access token for all pages when logging in as the administration developers.facebook.com account using the following code below which gives us the data array with all access_tokens as expected.
However the problem we have is when we try to login with the clients facebook account the array is empty.
So we tried to add them as an admin for the app which is not ideal as they are not developers so would not be happy with this approach.
I believe this is not working due to manage_page permission.
Does anyone know a way around this without having to submit the app for review?
$app_id = '1234567890';
$app_secret = 'ABCDEFGHIJKM';
$facebook = FacebookSession::setDefaultApplication($app_id, $app_secret);
$session = FacebookSession::newAppSession();
$callback_url = 'http://'. $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$helper = new FacebookRedirectLoginHelper($callback_url, $app_id, $app_secret);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookSDKException $e) {
echo 'FacebookSDKException Error: ' . $e->getMessage();
$session = null;
} catch(\Exception $ex) {
echo 'Exception Error: ' . $e->getMessage();
}
print "<pre>"; print_r($session); print "</pre>";
if ($session) {
// User logged in, get the AccessToken entity.
$accessToken = $session->getAccessToken();
// Exchange the short-lived token for a long-lived token.
$longLivedAccessToken = $accessToken->extend();
// Make calls to Graph with the long-lived token.
} else {
echo 'Login with Facebook';
}
FacebookSession::enableAppSecretProof(false);
try {
$request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
$pageList = $request->execute()
->getGraphObject()
->asArray();
} catch(FacebookRequestException $e) {
echo 'FacebookRequestException Error: ' . $e->getMessage();
} catch(\Exception $ex) {
echo 'Exception Error: ' . $e->getMessage();
}
print "<pre>"; print_r($pageList); print "</pre>";

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/

SDK 4.0 Facebook, no dialog to request permission publish_actions

I created an app on facebook and via PHP SDK 4.0 I created the login and I need to have write permissions on the feed, but even we have added the permissions, when I do not log apparre the more window box to approve and grant permissions .
this is the code used:
<?php session_start();
require 'Facebook/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('XXXXXXXXXX XXXXXXXX','XXXXXXXXXXXXXXXXXX');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'XXXXXXXXXXXXX' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
} else {
$params = array(
scope => 'publish_actions'
//redirect_uri => $url
);
// show login url
echo 'Login';
}
?>
publish_actions should be approved by facebook for your application if you have already submitted for approval facebook might take upto a week till then you can't publish stories on facebook user's timeline.

facebook api no longer working

<?php
session_start();
//included file and use
$app_id = 'xxx';
$app_secret = 'xxx';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper("`http://example/facebook4.0`/", $app_id, $app_secret);
try
{
$session = $helper->getSessionFromRedirect();
}
catch(FacebookRequestException $ex) { }
catch(Exception $ex) { }
$loggedIn = false;
if (isset($session))
{
if ($session)
{
$loggedIn = true;
try { //logged here and get data
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
print_r($user_profile); //print data
}
catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
}
if(!$loggedIn) //if user is not online // get link and add scope
{
$loginUrl = $helper->getLoginUrl(array('public_profile','email'));
echo "<a href='$loginUrl'>Login With Facebook</a>";
}
else
{
print_r($user_profile); //logout link is generated here
echo '<br><br>Logout'; //print to sceen
// i dont have more comment to write stack over flow please update it without eating my head to write comments here
}
?>
It was working from last week but not working today at all, after redirecting it display nothing
It can be that your appID and app Secret have changed. Try confirming that .

Integrating facebook php sdk in Facebook Canvas App giving Blank page or oauthData error

I am trying to integrate facebook for my canvas app. When i run app from facebook with following code
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xx','xx');
$helper = new FacebookCanvasLoginHelper();
try {
$data = array('oauth_token' => 'token');
$data['algorithm'] = 'HMAC-SHA256';
$data['issued_at'] = time();
$base64data = base64_encode(json_encode($data));
$rawSig = hash_hmac('sha256', $base64data, 'app_Secret', true);
$sig = base64_encode($rawSig);
$signedRequest = $sig.'.'.$base64data;
$_GET['signed_request'] = $signedRequest;
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
echo $ex;
} catch(\Exception $ex) {
echo $ex;
}
The entire page just turns blank white because of $_GET['signed_request'] = $signedRequest;.
What should I do to get login. If i just do $session = $helper->getSession(); instead of Get i get invalid signed paramters oAuth data missing.
Your PHP should be:
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
if($session){
try {
$facebook_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo $facebook_profile->getName;
} catch(FacebookRequestException $e) {
}
}
} catch(FacebookRequestException $ex) {
echo $ex;
} catch(\Exception $ex) {
$facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}';";
}
And then somewhere in your HTML:
<script>
<?php if(isset($facebookLoginHtml)){ echo $facebookLoginHtml; } ?>
</script>
If you want to ask for extra permission, add the scope parameter in the URL like this:
$facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}&scope=publish_actions';";
That will redirect the page to the login page, and then come back to your canvas app with the proper permission.
This shouldn't work like this as it's using Javascript with the PHP SDK. It's a bug that is being addressed by Facebook which you can follow here:
https://developers.facebook.com/bugs/722275367815777
I'll edit the answer if that bug ever gets resolved.
Thanks guys!
My approach:
<?php
session_start();
require ({your_php_sdk_path} . 'autoload.php');
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
FacebookSession::setDefaultApplication({your_app_id},{your_app_secret});
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
}catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if (!is_null($session)) {
// Logged in
try {
//Get user name
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
$user_profile_name = $user_profile->getName();
//Get user picture
$request = new FacebookRequest(
$session,
'GET',
'/me/picture',
array (
'redirect' => false,
'height' => '135',
'width' => '135',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
$user_profile_picture = $graphObject->getProperty('url');
} catch(FacebookRequestException $e) {
// When Facebook returns an error
} catch(Exception $e) {
// When validation fails or other local issues
}
}else{
//First time -> ask for authorization
$helper = new FacebookRedirectLoginHelper({your_canvas_url});
$login_url = $helper->getLoginUrl();
}
?>
And in your html put a javascript:
<script type="text/javascript">
if($login_url != null){
top.location.href = $login_url;
}
</script>
<?php
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('*********','*********' );
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(Exception $ex) {
// When validation fails or other local issues
}
if($session) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
}
else {
// show login url
echo 'Login';
}
?>

Categories