facebook php sdk 4.0.0 - php

I can't get user id with facebook php sdk :|. Anyone can help me ? this is the code.
I spend like 4 hours to get some results but nothing. with sdk 3.2.3 works fine but with 4 i can't get it to work.
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// 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) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
// Logged in.
}

Something like this should work. You have to go to your facebook developer panel and generate an access token for this app by clicking "You need to grant permissions to your app to get an access token."
Then your code should be something like this:
$session = new FacebookSession('your-token');
try {
$me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo $me->getName();
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}

I think your app is just not well initiated with your app ID and Secret.
But you can no more get the real user id with the new graph api and php sdk.
Read their changelog for details

Related

email is not coming in facebook php sdk

This is login system in facebook php sdk,what happens it asks for basic permissions then when a user permits through his facebook account it stores his facebook id and email and then shows this fbid and email in the index through session ,but it is not working properly though fbid comes nut email is not coming it is showing blank
<?php
session_start();
require_once("autoload.php");
require_once('Facebook/FacebookSession.php');
require_once('Facebook/FacebookRedirectLoginHelper.php');
require_once('Facebook/FacebookRequest.php');
require_once('Facebook/FacebookResponse.php');
require_once('Facebook/FacebookSDKException.php');
require_once('Facebook/FacebookRequestException.php');
require_once('Facebook/FacebookAuthorizationException.php');
require_once('Facebook/GraphObject.php');
require_once('Facebook/GraphUser.php');
require_once('Facebook/GraphSessionInfo.php');
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest; use Facebook\FacebookResponse;
use Facebook\FacebookSDKException; use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException; use Facebook\GraphObject;
// init app with app id and secret
FacebookSession::setDefaultApplication( ' 332325566667346','30adsfsdf7sdf87df6s7df87sdf76dsfd16' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://localhost/bb/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
}
// see if we have a session
if ( isset( $session ) ) {
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$email = $graphObject->getProperty('email');
echo $email;
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
You are not asking for the email permission in your login process:
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
Source: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl
Side note: Use the autoloader OR countless require statements, not both ;) (Autoloader would be the smart option though).

Facebook Connect PHP session variable won't set

I am using the Facebook PHP API 4.0 and trying to get the session variable to be set, so I can use the user's Facebook data.
The app, and Facebook approval page have all worked as expected, but the $session variable remains to be unset no matter what I try. I have tried to find the solution from similar questions on Stack with no success. This is the code I am using:
session_start();
//start fb login stuff
require_once( $config['basedir'].'/Facebook/FacebookSession.php' );
require_once( $config['basedir'].'/Facebook/FacebookRedirectLoginHelper.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequest.php' );
require_once( $config['basedir'].'/Facebook/FacebookResponse.php' );
require_once( $config['basedir'].'/Facebook/FacebookSDKException.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequestException.php' );
require_once( $config['basedir'].'/Facebook/FacebookAuthorizationException.php' );
require_once( $config['basedir'].'/Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($fb_app, $fb_secret);
$helper = new FacebookRedirectLoginHelper( $config['baseurl'] );
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
echo $ex;
} catch(\Exception $ex) {
// When validation fails or other local issues
echo $ex;
}
if ($session) {
echo 'Session var is finally being seen';
}
//end Facebook login stuff
The $session remains unset because you aren't logging the user in. You should modify the last part of the code to something like:
if ( isset( $session ) ) {
echo 'Session var is finally being seen';
} else {
// show login url
echo 'Login';
}
If the $session is not set, your code will ask the user to login. After they login, the $helper->getSessionFromRedirect(); part of your code should execute and set $session. See my tutorial on how to tackle this.
Edit:
If you are logging the user in from a different page, you need to make sure that $helper = new FacebookRedirectLoginHelper( '{your-url}' ); is the same on both pages. If they differ, then you will get a exception from the API related to redirect_uri mismatch.

Request app level information with Facebook-php-sdk

Hi i am trying to make a request with a app session and its saying this : "(#200) Must have a valid access_token to access this endpoint". I can make requests only with a app session right?
Below its the code its giving me this error.
<?php
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookPermissionException.php');
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php');
require_once( 'Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( $facebook_app_id, $facebook_app_secret);
$session = FacebookSession::newAppSession();
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
}
if(isset($session)){
try {
$response = (new FacebookRequest($session, 'GET', '/search?q=coimbra&type=event&limit=5000'))->execute();
$object = $response->getGraphObject();
echo "done.";
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
}
?>
The error means you must have a valid USER access_token to preform searches. This is because the search results will filter out events (or other content) the user isn't allowed to see, e.g. private events, or closed event. But it also means you find event the user does have permission to see.
In summary, you need to log the user in to get a valid user access_token for the search to work.

Any Facebook PHP SDK v4.06 working Canvas App Example? Cant Get a valid session

<?php
// include required files form Facebook SDK
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookCanvasLoginHelper.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookPermissionException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphLocation.php' );
include ("fns.php");
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
use Facebook\GraphLocation;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'XXXXXXXXXXx','yyyyyyyyyyyyyyyy' );
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (Exception $ex) {
// echo $ex->getMessage();
}
if($session)
{
$request = new FacebookRequest( $session, 'GET', '/me' );
try{
$response = $request->execute();
$graphObject = $response->getGraphObject();
//echo print_r( $graphObject, 1 );
$fid = $graphObject->getProperty('id'); echo $fid;
$femail = $graphObject->getProperty('email'); echo $femail;
$ffirst_name = $graphObject->getProperty('first_name'); echo $ffirst_name;
} catch (Exception $ex) {
// echo $ex->getMessage();
}
}
?>
First off, I am not an expert PHP coder, so I do not understand a lot of things, but I do know how to use a working example. I made a Canvas App that was working fine. Then I installed ssl and it no longer works. The code itself is a mess from my efforts to fix the problem.
Can I just get a working example that echos user public data using FacebookCanvasLoginHelper with the latest 4.06 PHP SDK? I can take it up from there if I can see that.
Since V4.0, getsession() no longer seems to work. In V4.06 its supposed to have been fixed, but I still cant get it to work.
Thanks in advance
Edit
I have added the relevant code. After debugging, i find that it appears $session is not valid or something because code execution never gets past "$request = new FacebookRequest( $session, 'GET', '/me' );". I cant seem to get anything echoed out after that. please any help is much appreciated.
If it worked previously and you ONLY changed to SSL then the only thing that would cause the error and lack of session is a redirect mismatch.
Your old app redirect would be http and the new app redirect will be https
Change the Facebook app to reflect this change in the app redirect set up.
php 5.4 is required.
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /var/www/Facebook/FacebookResponse.php on line 137

PHP Facebook SDK 4.0 post as a page server to server

I'm trying to post as a page on a Facebook page since 6 days without success, which is really frustrating using new facebook SDK 4.0.4.
I'm under PHP 5.4 (as requested by Facebook 4.0 SDK version).
I already read some times all the facebook documentation (which isn't so clear) https://developers.facebook.com/docs/reference/php/4.0.0.
For logging-in:
<?php
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSession.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookResponse.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookOtherException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookAuthorizationException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphSessionInfo.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookServerException.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\FacebookServerException;
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication($site["facebook_appid"],$site["facebook_appsecret"] );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($site["url"].$_SERVER['REQUEST_URI']);
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
echo 'Logout';
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
echo 'Login';
}
///////////////////////////
// SHORT LIVED TOKEN
$request = new FacebookRequest( $session, "GET", "/".$site["facebook_appid"]."/accounts?access_token=".$site["facebook_appid"]."|".$site["facebook_appsecret"]."&grant_type=client_credentials" );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
if (DebugMode) e($graphObject);
$site['facebook_short-lived_access_token'] = $graphObject['data'][0]->access_token;
////////////////////////
///////////////////////////
// LONG LIVED TOKEN
$request = new FacebookRequest( $session, "GET", "/oauth/access_token?grant_type=fb_exchange_token&client_id=".$site["facebook_appid"]."&client_secret=".$site["facebook_appsecret"]."&fb_exchange_token=" .$site['facebook_short-lived_access_token']);
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
if (DebugMode) e($graphObject);
$site['facebook_long-lived_access_token'] = $graphObject['access_token'];
$_SESSION['fb_token'] = $site['facebook_long-lived_access_token'];
if (DebugMode) e($site['facebook_long-lived_access_token']);
?>
And for publishing:
<?php
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookPermissionException.php' );
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
use Facebook\FacebookSDKException;
use Facebook\FacebookPermissionException;
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', "/".$site["facebook_page_id"]."/feed",
array(
'access_token' => $_SESSION['fb_token'],
'link' => $site["url"].'/url',
'name' => 'link name',
'caption' => 'link caption',
'description' => 'link description',
'picture' => $site["url"].'/alpha.png',
'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();
}
}
?>
I first get a session ID, then request and obtain a short-lived token, with which i request and obtain a long-lived token (60 days validity). Then I continue with the publish script using the last obtained token.
I submit a post and it return back a post-id like it works, but nothing on the page, nothing in my personal Facebook history.
My application is in development mode and was not reviewed by Facebook, but it seems to not be necessary anymore: http://www.nextscripts.com/known-issues/facebook-issues/
Any idea where I'm wrong?
I'm not realy into the facebook SDK, but I noticed one line that I suspect to be incorrect:
echo 'Login';
The facebook documentation about this function can be found here.
It states that you schould, to ask for extra permissions, give an array with the index 'scope' and as value in string with a comma between every permission you want to request instead of an array with indexes called to permission but without any data in it. I suspect it should be something like:
$helper->getLoginUrl( array( 'scope' => 'public_profile,email,user_friens,publish_stream,<and so on>'));
I hope this helps you.

Categories