Pass email and user birthday scope in Facebook Graph API - php

I am new in Facebook Graph API..
I am using this code and it is fetching name and gender perfectly. I want to get email and date of birth from facebook.
<?php
// 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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxxxxx','xxxxxxxxxxxxxxxxxx' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://www.xxxxxxxxx.com/' );
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();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php");
}else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
Now how I pass email and user birthday scope in my code.
I already take the approval of User birthday and email from facebook viva my APP.

$permissions = ['email', 'user_birthday'];
$loginUrl = $helper->getLoginUrl('http://{your-website}/login-callback.php', $permissions);
Source: https://developers.facebook.com/docs/php/FacebookRedirectLoginHelper/5.0.0
Btw, the redirect URL is a required parameter anyway.

Related

Login with Facebook error:"Fatal error: Uncaught Error: Class 'Facebook\FacebookSession' not found in[..]"

I am seeing the following error:
"Fatal error: Uncaught Error: Class 'Facebook\FacebookSession' not found in [...]"
My code is like this:
session_start();
require_once("../../src/composer/vendor/facebook/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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( '****','****' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://localhost/signup/withfacebook/' );
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();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php");
}else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
I use the facebook graph api v5 downloaded with composer from: "https://github.com/facebook/php-graph-sdk?fbclid=IwAR09B8JjgOMlCQTm5x4gdxbQmLjMHVVsUeepx9_2pAvO9LImDX8MYic8TMo"
It's the official facebook github page.
Can you make sure the location of autoload.php is correct?
Can you write;
echo "example";
die();
into autoload.php?
It writes an example in white on the screen and stops working
Sample Output
The autoload.php location you specified is incorrect if it does not display an "example" on the screen.
change require_once position until you find the correct position, that is, until you see "example" on the screen

fb login helper error

I did everything perfect but when i click on the link of the login helper it redirects me to the facebook page and gives this error:
Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
Any suggestions?
my facebook developer app settings
<?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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// start session
// init app with app id and secret
FacebookSession::setDefaultApplication( '367245673760849','my secret goes here' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://saturnproduction.com/test.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
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbuname = $graphObject->getProperty('username'); // To Get Facebook Username
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['USERNAME'] = $fbuname;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
// show login url
echo 'Login';
}
?>
You have to input all the URLs in Valid OAuth Redirect URIs.
API Documentation : https://developers.facebook.com/docs/facebook-login/security/#surfacearea
Go to App Dashboard, then under Products /Facebook Login /Setting
Link : https://developers.facebook.com/apps/App_ID/fb-login/settings/
Replace it with your App_ID = 367245673760849
According to the code sniped mentioned above URL should be http://saturnproduction.com/test.php
Refer Screenshot if any doubts:

FB login website integration using php sdk

Good day guys, i have an issue with my facebook login using php. i have done everything appropriately i assume but still get errors. I'd love some with more knowledge to help me resolve this.
Below is the error page:
<?php
session_start();
ini_set('display_errors', '1');
// added in v4.0.0
require_once 'autoload.php';
require 'functions.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\EntitiesAccessToken;
use Facebook\HttpClientsFacebookCurlHttpClient;
use Facebook\HttpClientsFacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( 'APP ID','APP SECRET' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://www.xxxxxxxx.com/3rd_party/fbOAuth/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
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
checkuser($fuid,$ffname,$femail);
header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
Thanks for the assist everyone. I have been able to resolve the issues using the SDK v5.4 and changing the paths and redirects.

Facebook connect API : Get user email

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.

Get Facebook User Email Using Graph API PHP SDK

I'm Using a Graph API of Facebook with php i'm trying to get user information to login user to my site but after a lot of searching and reading i'm getting all my required data expect email.
<?php
include_once('../dashboard/includes/db_connection.php');
// 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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication( '1393606834280204','3faf1ede6ccc48d6adf096f1a6850359' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://lhefficise.com/facebook/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?fields=id,first_name,last_name,name,email' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// $fbid = $graphObject->getProperty('id'); // To Get Facebook ID
// $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
// $fbfirstname = $graphObject->getProperty('name'); // To Get Facebook First Name
// $fblastname = $graphObject->getProperty('name'); // To Get Facebook First Name
// $femail = $graphObject->getProperty('email'); // To Get Facebook email ID
$obj = $graphObject->asArray();
echo "<pre>";
print_r($obj);exit;
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
?>
i'm sending request for /me?fields=id,first_name,last_name,name,email but i'm not getting email all other information is fine.
Array
(
[id] => xxxxxxxxxx83xxxx
[first_name] => Shan
[last_name] => Khaliq
[name] => Shan Khaliq
)
I might be doing something wrong while getting the record..
I already have permission for email check the link for that.
http://prntscr.com/9steek
Thanks in advance.
I just figure it out just after i posted this.
$loginUrl = $helper->getLoginUrl(array('scope' => 'email'));
i need to put scop to get login url from facebook.
guys Thanks i really appropriate who were upto this question.

Categories