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

后端 未结 3 963
逝去的感伤
逝去的感伤 2021-01-20 02:30

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 f

相关标签:
3条回答
  • 2021-01-20 02:37

    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

    0 讨论(0)
  • 2021-01-20 02:42
        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>
    
    0 讨论(0)
  • 2021-01-20 02:56

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

    0 讨论(0)
提交回复
热议问题