Facebook connect API : Get user email - php

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.

Related

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 LIKE Button Using Facebook Graph API

I am working on a website in which there are more than three Facebook Page Like Button. Once user click on the button. First, I need to authenticate the user and then like the respective page. However User authentication is working fine now I need to Like a page. I am following the Facebook Like Documentation. But I am getting confused with {object-id} metioned in the documentation. What is this object-id and How do I get this ?
Like Page Code :
session_start();
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;
FacebookSession::setDefaultApplication( 'App_ID','App_Secret' );
$helper = new FacebookRedirectLoginHelper('http://localhost/fb/like.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// Facebook returns an error
} catch( Exception $ex ) {
// validation fails or other local issues
}
if ( isset( $session ) ) {
// Get Basic Userinfo
$getUserInfoObj = new FacebookRequest( $session, 'GET', '/me' );
$getUserInfoProcess = $getUserInfoObj->execute();
$getUserInforesult = $getUserInfoProcess->getGraphObject();
$id = $getUserInforesult->getProperty('id');
// Once Use Logged In Like the respective page
// Like page code
echo '<pre>';print_r($getUserInforesult);exit;
//header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
Object Id is your page, comment, post even photo id that want to make liked.
For example stackoverflow facebook page link is,
https://www.facebook.com/pages/Stack-Overflow/105665906133609?fref=ts
so the object_id is 105665906133609

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.

Access forbidden facebook login

I have a problem. I am currently working on a php-project and I implemented a facebook-login system. I redirected my users to a file on my local host. However, When I try to login i get this error:
You don't have permission to access /rent-a-Student/codeigniter/application/views/users/User_view.php on this server.
In the console I see:
GET http://localhost/rent-a-Student/codeigniter/application/views/users/User_vi…WK9-EQOdn475FhwznNhICtrlSMqO6t3c7qp&state=3beb2184e0ca8bf17289be263cc83ddf 403 (Forbidden)
this is my login_fb file:
<?php
// start session
session_start();
// loading facebook srcfiles
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;
// initialise app, helper and get session
$app_id = '480209908792754';
$app_secret = '86e572d72f8720363b0c01552953e064';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$helper = new FacebookRedirectLoginHelper('http://localhost/rent-a-Student/codeigniter/application/views/users/User_view.php');
$session = $helper->getSessionFromRedirect();
// if session exists ,get user info
if(isset($session)) {
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graph = $response->getGraphObject();
$_SESSION['id']= $graph->getProperty('id');
$_SESSION['name']= $graph->getProperty('name');
$_SESSION['email']= $graph->getProperty('email');
header("Location: ../views/users/User_view.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
On the facebook developer site, I linked everything correct (I think).
I hope you guys can help me with this.
Never see the error message. But I tested the code and saw that facebook redirect to the correct url (although not exist in my computer), so the problem should be caused by the codeigniter url redirect mechanism.

Categories