-2

I created a Facebook login button for my site and trying to get user's Facebook email address. The array I get in response do not have email address.

Here is the code I'm using :

require_once '../../config.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($appid,$appSecretKey );
// login helper with redirect_uri
$helper = new    FacebookRedirectLoginHelper(WEBSITE_FRONTEND_URL.'/facebook/fbconfig.php' );
try 
{
  $session = $helper->getSessionFromRedirect();

} catch( FacebookRequestException $ex ) {
  //echo " When Facebook returns an error";
} catch( Exception $ex ) {
 // echo "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' );
    print_r($request);
   $response = $request->execute();
  // get response
   $graphObject = $response->getGraphObject();
  print_r($graphObject);
   echo $graphObject->getProperty('email');
 }
 else 
 {
    $loginUrl = $helper->getLoginUrl(array('email'));
    header("Location: ".$loginUrl);
 }

How do I get the email address?be able to get email address.

vhu
  • 12,244
  • 11
  • 38
  • 48
Simranjeet Kaur
  • 259
  • 1
  • 6
  • 18
  • Is your app using API v2.4? In that case, go read the changelog – the number of fields returned by default has been reduced drastically, and you have to specifically ask for additional fields now when making your API request. – CBroe Jul 31 '15 at 13:30
  • How to check that my app using API v2.4? – Simranjeet Kaur Aug 01 '15 at 03:56

2 Answers2

1

If you're using v2.4 of the Graph API, you'll need to explicitly specify each field you want to have returned from the API:

$request = new FacebookRequest( $session, 'GET', '/me?fields=id,name,email' );

Have a look at my answer here:

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • Nope.. It still not returning me email address. Anything else i can do to get email? – Simranjeet Kaur Jul 30 '15 at 06:41
  • You need to request the permission via the Login scope. – Tobi Jul 30 '15 at 06:42
  • I already added scope $loginUrl = $helper->getLoginUrl(array('email')); It ask for email permission but would not return email in response. – Simranjeet Kaur Jul 30 '15 at 06:44
  • Make sure you logout of your application before logging in again. And, you can debug the access token whether it really contains the permission: https://developers.facebook.com/tools/debug/accesstoken/ – Tobi Jul 30 '15 at 07:01
  • I am trying from another facebook account not from the account in which i created the app. So i am checking it in after logged out from the app. – Simranjeet Kaur Jul 30 '15 at 07:04
  • Sir please help me out from this problem. i wasted my 5 hour to solve this issue but still its not solved.. – Simranjeet Kaur Jul 30 '15 at 07:12
  • Do what I wrote. Does the access token contain the permission? – Tobi Jul 30 '15 at 07:13
  • I am getting (facebook/fbconfig.php?code=AQD01fRSS1zMRbxr7iws_ukp3Nph9O_Egasae-sPozyrRwdHcZ4w9eaEBIOJ1CBf4S_qg_UG-iHeWD9Wmf08CaR94jcxPfItS8_HDxEs2H5I4YFZCQ_a5fEFsOLVN1u-NtBk6lcrZ3ZAVUy2jvfhTZ3d3228qRZcI1WDlCSu_ikQvZxGtyV0JBTTvngQ90vudd6OtRzhbxZMUqZ1KCMFMVzlZnXgS3irlnPYCMEMYewU3lXfxLkx-2A8XfpIMa8lU9BJbxHT6DqMy4HP0Hj_6CxY-UhPRIo3rz7Nfb0cr9p3WB4nVDMEbLQK8fpd4wsIrEmAy4SOhjoiO70hsT0a7UKN&state=3f48d310074a93543e37b220d8676726#_=_) url in response i don't know which is access token. – Simranjeet Kaur Jul 30 '15 at 07:18
  • That''s only the code, not the access token... Try `echo $helper->getAccessToken();` in your code. See https://developers.facebook.com/docs/php/howto/example_facebook_login/5.0.0 – Tobi Jul 30 '15 at 07:22
  • Yes i found the function getAccessToken() but it returns me an error "Non-static method Facebook\FacebookSession::getAccessToken() should not be called statically " – Simranjeet Kaur Jul 30 '15 at 07:29
  • Yes i am getting access token in response as shown in the link http://stackoverflow.com/questions/26430111/how-to-access-facebook-profile-picture-using-facebook-php-sdk – Simranjeet Kaur Jul 30 '15 at 07:37
-1
        FB.login(function (response) {
            if (response.session) {
                var url = '/me?fields=name,email';  // this is what you want
                FB.api(url, function (response) {
                    alert(response.name);
                    alert(response.email);
                });
            }
            else {
                alert("User did not login successfully");
            }
        }, { scope: 'email' }); /* perms changed to scope */

use this as a url

var url = '/me?fields=name,email';

Amit Rana
  • 190
  • 5