Facebook user email extended permissions with new SDK - php

I am currently using the code below in my Codeigniter application to get Facebook data from an app user.
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '2453463636',
'secret' => '365653656565656',
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
// $user_profile = $facebook->api('/me');
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
echo $user_profile['email'];
echo $user_profile['username'];
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
?>
This works fine in connecting and returns the username of the user echoed on the page, however the email does not work. The email returns shows up as an error on the view "Undefined index: email".
I used this very same thing to get the email just a few days ago and it worked, however that was on localhost. It get the email fine on localhost, when pushed to the server, it doesn't recognize email, but it does recognize and get the username.
Why isn't the email being recognized?
I am using the latest version of the sdk: Facebook PHP SDK (v.3.2.3)

You can't do with this options. You need extended permission from Facebook. Check this link for more details:
http://developers.facebook.com/docs/authentication/permissions/
Facebook Doesn't share user email without their permission.

Related

Apache2 not executing facebook's php sdk

I have just started mingling with facebook's php sdk, but to no avail. Although php works fine for all the other stuff on my ubuntu instance, facebook's php sdk doesn't (i.e. php code is not executed). All dependencies (only curl and json) are enabled and working.
I tried to look into /var/log/apache2/error.log but it seems that the file is empty - but maybe I'm not looking in the correct place.
So my question is: what would be the steps for me to debug this problem! Thanks for helping a noob!
Here is the boilerplate index.php code for from facebook:
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>

Facebook SDK (/me) does not contain email although permission is given

I am using the PHP Facebook SDK on my website where users can connect there Facebook accounts. I want to get the user's email address. Permission is given by the user. But if I output facebook->api('/me') then there is no e-mail address. For all other extra information like 'user_likes' it works all fine.
Does anyone have an idea how to solve this?
Here is my code. I am using Codeigniter
$fb_config = array(
'appId' => 'xxx',
'secret' => 'xxx'
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
$user = null;
}
else {
$params['scope'] = "email";
$data['login_url'] = $this->facebook->getLoginUrl($params);
}
print_r($data['user_profile'];
Thanks!
Did you try with another account? I had a similar problem once, the e-mail wasn't displayed for a specific user, and it worked well for others.
I ended up using username#facebook.com as an e-mail for this specific case, but never found out why it didn't work for this specific user.

Facebook Login Doesn't Work

I am trying to add Facebook login to my web site.
What I have done?
Created APP on Facebook as Website with Facebook Login
Sandbox mode is off
APP is set to work on localhost.
What is the problem?
I keep getting $user variable's value as 0
example.php in Facebook PHP SDK works just fine!
I can see my APP when I visit Facebook -> Privacy Settings -> Apps. No matter if I login with my login.php or example.php of Facebook PHP SDK, the app seems to be added just the same. However while example.php can retrieve data from facebook, login.php can't.
I copy and paste the codes from example.php to my login.php page (please check the code below).
Here is my code (login.php);
require_once('system/api/facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => '123456',
'secret' => 'abcde12345',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
if (!$user) echo 'Login';
echo '<pre>';
var_dump($user);
echo '</pre>';
Can someone please tell me what is the difference between my login.php and example.php? I also Googled and searched here without finding any info which was helpful.

Facebook Application get user information

i am trying to write a Facebook application where once installed on a page provides a simple Form once this form is populated it would create a new table with this users username or userid.
So far i have the FBML form up and running using an external php file to process it all how can i get users username or userid to my forms php action file.
Are you using the PHP SDK or the javascript SDK? If you're using the PHP SDK, it's as simple as this:
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
$user = $facebook->getUser(); // This is the FB user ID
$username;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$username = $user_profile['username']; // this is the facebook username.
// If the user hasn't set it manually, it will be something
// like "john.doe.50"
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}

How to get the email address of a Facebook user using the graph API?

I have read through this post over and over, and yet it doesn't make sense to me.
What I am trying to do is acquire a Facebook user's email address so that I can store it in a string and compare to see if it matches the emails of existing users in my MySQL database. I only need to to do this when the user first grants access rights to the app.
In the "Authenticated Referrals" section of my app developer interface, I have set "User & Friend Permissions" to include "email".
Within the "canvas" area of my site, I have this code:
include('facebook/base_facebook.php');
include('facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => "XXXXXXXXXXXXXXX",
'secret' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
));
$user = $facebook->getUser();
if ($user)
{
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
If I var_dup() $user, I can see the user id (though not much else) which seems to be correct, so I believe I'm connecting to the right place on Facebook.
But then there was this suggested line of code, which implies that it returns an email address, but when I echo it out to see what it looks like, it doesn't return an email address:
echo "<br/>" . $facebook->getLoginUrl(array('req_perms' => 'email'));
I kind of thought it wouldn't, as it doesn't look quite right to me, but on the other hand, I just have no idea what else to do with it or where to put it.
And then I also came across this post, which has code mixed in with the HTML in a way that baffles me even further.
Assuming that my user has granted permission for my app to see their email, isn't there a simple way of getting at it? Something an inexperienced programmer like myself can grasp?
You are using outdated method. You can use the code below to retrieve the email.
You can find more information about Facebook Connect from : http://25labs.com/tutorial-integrate-facebook-connect-to-your-website-using-php-sdk-v-3-x-x-which-uses-graph-api/
$app_id = "xxxxxxxxx";
$app_secret = "xxxxxxxxxx";
$site_url = "xxxxxxxxxxxxx";
include_once "src/facebook.php";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
As suggested in comments you should replace req_perms with scope since it is the name for field indicating which permissions to be granted by users.
Once email permission is granted (do you see it's asked in the Auth Dialog?) you'll be able to get email field via Graph API. You can also retrieve only required fields in results by specifying 'em in the fields parameter:
$facebook->api('/me', array('fields' => 'id,email'));
BTW, Please not that both posts you refer to are old (2010), way many things changed in the platform. Be sure to read updated documentation on Permissions and Authentication
You can concatenate a Facebook username with the e-mail address. For example, my username is "umair.hamid.79" on Facebook. It will be "umair.hamid.79#facebook.com".

Categories