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

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.

Related

Facebook user email extended permissions with new SDK

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.

Trouble with Facebook Api login $user is not returning properly

When I run this code, and my cookies have been cleared and I am not logged into facebook. It directs me to facebook and I log in but when it brings me back to my page it is the same, where as it should be show me profile pic and so on...
I seem to have narrowed down the problem of the return statement of the $user because it returns a 0. I have stared at this code for a long time and I havent found what I am doing wrong.
What do i need to change to get it so that When i return from logging in from facebook it will show the profile pic and so on...
<?php
require_once 'libs/facebook.php';
require 'connections/connection.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
echo $user;
} 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();
$logoutUrl = $facebook->getLogoutUrl(array('next' => ($fbconfig['baseurl'] . 'logout.php')));
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me',
'scope' => 'read_friendlists'
));
}
?>
Not to worry my friend, you are not doing anything wrong. I'm having same issue with my DEMO application which was running alright till yesterday.
I'm sure you are having issue with getting access token as well. I think issue is with PHP SDK only.So probably will be fixed in some time.
SOLUTION : This worked for me after trying for many solutions for this issue.
In base_facebook.php file, find the makeRequest() method and check for following Line.
$opts = self::$CURL_OPTS;
Immediately following it, add this line
$opts[CURLOPT_SSL_VERIFYPEER] = false;
More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

Facebook PHP Get User information in Page Tab, getUser returns 0 after redirect to login

I am trying to create a competition entry which checks to see if the user likes a page before entering. If they do, it will fetch their name, email and birthday for the competition entry details.
For some reason however, I cannot get my PHP code to work.
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXXX',
'cooke' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook->api('/me?fields=likes.target_id(215359398497970),birthday,first_name,last_name,email');
if($me){
$first_name = $me['first_name'];
$last_name = $me['last_name'];
$email = $me['email'];
$dob = $me['birthday'];
die($me);
} else {
$likes = false;
}
} catch (FacebookApiException $e) {
die($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();
//echo $loginUrl;
}
?>
For some reason, it gets stuck in a redirect loop because the loginURL doesn't seem to log my user in.
For anyone who's still looking. This is how I fixed my problem. I had to ensure that theis code was executed BEFORE any other code so I plonked it to the very top of my php file and it worked. I think the problem was that the headers had already been sent but I was unaware because my PHP warnings / errors are not enabled to show up.
I've encountered the same problem. I used an example project, and couldn't figure out why it keeps returning 0. tried nearly all related links here but none solved it.
The solution was to update Facebook's SDK files in the example project.
Simple and easily missed,

Post to fan page as admin worked yesterday, not today

To post to facebook fan page, using a php script which is called by a cronjob, im using the following code, which worked yesterday while testing, it is no longer working.
include_once("../facebooksdk/src/facebook.php");
$facebook = new Facebook(array(
'appId' => 'XX',
'secret' => 'XX',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = 'XX';
$page_info = $facebook->api("/".$page_id."?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "Welcome to TuneHub!"
);
$post_id = $facebook->api("/".$page_id."/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
I cant figure out why it would work temporarily, then the following day when the code has been added to the live site, its no longer working (on the test or live site, it isnt working)
is there an API change that is killing the function?
or is there something I am doing wrong with the code that flagged Facebook to prevent it from posting?
(the code may have changed slightly from the script i had which was working, as ive been fiddling around with it to try and find the issue)
see
https://developers.facebook.com/docs/reference/api/page/#posts
Check your access token it may be expired, you need to have a valid access token with rights to post on wall, try getting the updated access token and try

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