Hello I am working with the sdk v4 facebook and when I login it does not redirect but generates the code ( In low image Link when I log ) Someone can help ?
Image
Code
<?php
session_start();
// Define the root directoy
define( 'ROOT', dirname( __FILE__ ) . '/' );
// Autoload the required files
require_once( ROOT . 'autoload.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookSDKException;
//Dados API
$app_id = '346646635512123';
$app_secret = '21h3vj12i312b3hj123';
$redirect_uri = 'http://localhost:8888/Facebook/';
// Permissoes
$permissions = array('email', 'user_location', 'user_birthday', 'manage_pages', 'publish_actions', 'user_photos');
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_uri);
try {
$session = $helper->getSessionFromRedirect();
} catch(Exception $ex) {
// When validation fails or other local issues
}
if (isset( $session)) {
echo'Estou Ligado';
}
else
{
echo "Login With Facebook";
}
?>
You donĀ“t have any redirection in your code, that is why. You need to click on that login link to get to the authorization screen. I think you misunderstood "FacebookRedirectLoginHelper", it is usefuly AFTER a redirection from the authorization screen.
I would suggest using the JS SDK for authorization though. Much easier to handle, no redirection needed, no PHP needed, ...
Related
I am working on a website in php where i have integrated a Facebook Connect API which enabled the Facebook login button.
My code to get user details looks like this:
<?php
session_start();
// added in v4.0.0
require_once 'autoload.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;
use Facebook\GraphUser;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
$appID = 'app id';
$appSecret = 'app secret code';
FacebookSession::setDefaultApplication( $appID, $appSecret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://localhost/test/facebook_new/fbconfig.php' );
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
$profile = $response->getGraphObject('Facebook\GraphUser');
var_dump($profile);
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
After eecuting this code, var_dump($profile) displays only the User Facebook ID and his Full Name as displayed on the picture below:
What I want to get is the email. The email can be accessed through 'Facebook\GraphUser' but I really dont know what is wrong with my code.
Kindly help me solve this problem
$request = new FacebookRequest($session, 'GET', '/me?fields=name,email');
It is called "Declarative Fields", you have to specify the fields you want to get returned since v2.4 of the Graph API.
Also, make sure the user is authorized with the email permission.
Facebook users in their settings can set their email as private or public. This might be the reason it is not showing up for you.
I'm making a PHP application running on Google App Engine, and I'm trying to implement a Facebook Login.
After accessing to the login webpage, I am redirected to Facebook, and after I accept to log in for the application and I get redirected, I get the error "Error : Option 10065 is not supported by this curl implementation."
I have curl_lite enabled, and I'm running it on localhost for the time being. Here's my code:
<?php
session_start(); //Session should be active
#region Settings
$app_id = 'xxxxxxx'; //Facebook App ID
$app_secret = 'yyyyyyy'; //Facebook App Secret
$required_scope = 'public_profile'; //Permissions required
$redirect_url = 'http://localhost:8080/signup_fb.php'; //FB redirects to this page with a code
#endregion
#region Imports
//include autoload.php from SDK folder, just point to the file like this:
require_once "../libraries/facebook-php-sdk-v4-4.0-dev/autoload.php";
//import required class to the current scope
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
#endregion
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
//try to get current user session
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
die(" Error : " . $ex->getMessage());
}
if ($session){ //if we have the FB session
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
//do stuff below, save user info to database etc.
echo $user_profile->getProperty('name');
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
echo 'Login with Facebook';
}
Thank you for your answers :)
It's understandable that with curl_lite enabled (curl interface backed by urlfetch and with fewer features) you would see such an issue. Check the docs as another commenter mentioned, and you should definitely be using the real curl if you intend to run such specific advanced functionality with your box's devserver as the client to facebook's login flow.
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.
I am a PHP developer and what I want is firstly user come to my website and login using Facebook login then I want to redirect user to specific page and get access token and then call to Graph API to access user's data. I have divided it into 2 steps
1) Make user to login using Facebook, It's done and code is given below:
include_once('facebook-php-sdk-v4-master/src/Facebook/FacebookSession.php');
include_once('facebook-php-sdk-v4-master/src/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookRequest.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookResponse.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook-php-sdk-v4-master/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook-php-sdk-v4-master/src/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;
session_start();
FacebookSession::setDefaultApplication('APP id', 'APP key');
$helper = new FacebookRedirectLoginHelper('http://redirect-url.com/');
$loginUrl = $helper->getLoginUrl();
echo "FB login here";
2) Get token detail on redirect URL, which is incomplete
Can anyone please help me how can I get this detail ?????
See the following tutorial on how to handle login and sessions correctly.
To get the token after redirect, you need to so the following:
$session = $helper->getSessionFromRedirect();
Then, check if the session is valid and redirect as needed:
if ( isset( $session ) ) {
// user is logged in, display logout link
echo 'Logout';
} else {
// show login url
echo 'Login';
}
Even I was going getting the same error.
Found another method to get session information on another page.
On first page
FacebookSession::setDefaultApplication($app_id,$app_secret,$cookie);
// login helper with redirect_uri
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
$_SESSION['token'] = $session->getToken();
}
On the second page it should be
$token = $_SESSION["token"];
$session = new FacebookSession($token);
I am trying to get Facebook login with their new PHP SDK 4.0 working for my site. I followed their gist almost verbatim and still can't even get a test page to work. When I try to log in, I get redirected to a url of the form:
https://www.facebook.com/v2.0/dialog/oauth?client_id={some number}&redirect_uri{localhost%2F%7E{MyName}%2F{my site}}&state=08d94ec4670256aa2b2c586781590766&sdk=php-sdk-4.0.0&scope=
I have filled out the same url on my Facebook developer page already, and this is the code I am trying to test:
<?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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( '{My app ID}','{My app secret}' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}' );
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 '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
// show login url
echo 'Login';
}
Unfortunately, I never get redirected back, or even load the login page. What could I be doing wrong here?
I had the same problem.
You need to set a domain URL and site URL in your Facebook App settings first. Go to this page to change your setting: https://developers.facebook.com/apps.
I have shared a step by step here.
You don't have to include the php files. Facebook wants you to use composer to manage what is required.
However.... they give you a workaround: Include autoload.php and keep the file structure they give you for easiest implementation. For ease, put all the files from the SDK into a folder called facebook, so you have a sub folder in there called src and within that another one called Facebook
Then the SDK creates a link that your users can click on to authorize access to your app.
If you are testing, you need to keep deleting the app from your facebook profile to get the signup window each time (Settings top right dropdown, then Apps on left as of Aug 2014), otherwise it will bounce straight through to your specified redirect page and will appear as if nothing worked.
// if you include this file you don't need to use composer
require_once('facebook/autoload.php');
// use these
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
// initiate the session
FacebookSession::setDefaultApplication('[YOUR ID]', '[YOUR SECRET]');
// use the helper
$helper = new FacebookRedirectLoginHelper('[YOUR REDIRECT URL]');
// get a loginurl
$loginUrl = $helper->getLoginUrl();
echo "<a href='".$loginUrl."'>Link here</a>";
Notes to check:
Check you have set up a test app on facebook.
Check the domain matches the one you are using.
Check the full url for the redirect page that you will be using to handle the response.
The script is not complete. You have to write the else part where if the session is not set. Request for permission comes to role here.
else {
$helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/yourappname/');
$permissions = array(
scope =>'publish_actions',
'email',
'user_location',
'user_birthday'
);
// Get login URL
$auth_url = $helper->getLoginUrl($permissions);
//$auth_url = $helper->getLoginUrl(array('email'));
echo "<script>window.top.location.href='".$auth_url."'</script>";
As stated in the Facebook Developer documentation, you need PHP 5.4+.
Is this the code you are using?
$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}' );
You will be redirected from Facebook, so the argument here needs to be a URL accessible from the Internet.
I was having problem when trying to execute
$loginUrl = $helper->getLoginUrl();
The solution was to call:
session_start();
before the
$helper = new FacebookRedirectLoginHelper($redirect_url);
on #DigitalVisual's answer above.
$helper = new FacebookRedirectLoginHelper('localhost/~{My Name}/{My Project}');
Use like:
$helper = new FacebookRedirectLoginHelper('http://example.com/help.php');
Must use full URL.