murvinlai
murvinlai

Reputation: 50345

How to get the user email address in Facebook (Facebook Connect or Graph API)?

I previously have an FBML application in Facebook and now change to an IFrame application in FB, using the graph API. (under the same name, same game account. Just switch from FBML to iFrame).

I had a list of people with Extended Permission accepted for email. Since I change to IFrame, I can get their email... by what the API is told.

However, here is the problem.

1) When it was in FBML, I don't have their email. Only with their email permission. (that's what I was bounded)

2) I can get the user's email when they login and visit my application

3) However, I cannot get the user's email when they don't visit my application. Because some of them may not come back but I want to tell them about the update.

4) I tried to run the application using the email address I register for the application (i.e. the developer account), but still, I cannot retrieve their emails.

It seems that the application can only get the current login user. I cannot get their email even I'm the admin.

Is there any way I can do that?

For example: If a user 12345678 and go to my application, i can run this can get his email:

 $fql    =   "select  email from user where uid=12345678";

Obviously, that user cannot run and get information for user 23456.

e.g. $fql    =   "select  email from user where uid=23456";   <--- Fail

So, here is the problem come. I have thousands of people who previously signed up and accept the extended permission, how can I get their emails now? I cannot run this...

$fql    =   "select  email from user where uid=1000001";
$fql    =   "select  email from user where uid=1000002";
$fql    =   "select  email from user where uid=1000003";  
// assum 1000001 ... 1000003 are those users. 

I tried to login facebook using my dev account and do that, but, still it won't work. Appreciate if anyone has the solution. thanks.

Upvotes: 3

Views: 22335

Answers (3)

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

    You can get  user email address and details in Facebook (Facebook Connect or Graph API)
<?php

require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
      echo "FacebookApiException";
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { ?>
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre>
    <?php } else { ?>


      <div class="fb-login-button" data-scope="email,user_checkins">
        Login with Facebook
      </div>
    <?php } ?>
    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>

Upvotes: 1

user2015469
user2015469

Reputation: 64

First, make sure that you include email in the scope of permissions like so:

https://www.facebook.com/dialog/oauth?client_id=595603433841467&redirect_uri=http://127.0.0.1:8000/faceb_login/&scope=email

Next retrieve the oauth token as documented elsewhere. Then, to actually retrieve a person’s email, you can make the following request:

https://graph.facebook.com/1162321772?fields=email&access_token=XXXXXXXXXXXXXXXXXXXXXX...

The documentation for the graph api is here:

https://developers.facebook.com/docs/graph-api/using-graph-api

Upvotes: 0

Domenic
Domenic

Reputation: 112817

The idea is to get the user's email when he logs in, then store it in your own database for future reference.

Upvotes: 1

Related Questions