简体   繁体   中英

Facebook Connect API issue with authentication token

I have implemented the following code below according to the documentation and can get it to connect and display user id...

<?php

define('FACEBOOK_APP_ID', '87939462878');
define('FACEBOOK_SECRET', '1ab6346bc51329831998985aba200935');

function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

?>

<?php if ($cookie) { ?>
      Your user ID is <?= $cookie['uid'] ?>
    <?php } else { ?>
      <fb:login-button show-faces="true" perms="email,user_birthday,user_location"></fb:login-button>
    <?php } ?>


<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
               cookie: true, xfbml: true});
      FB.Event.subscribe('auth.login', function(response) {
        window.location.reload();
      });
    </script>

<?php
$user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']))->id;
print_r($user);
?>

However, if I use:

$user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?wrap_access_token=' .
    $cookie['oauth_access_token']))->me; //or $cookie['access_token']))->me;
register_user($user->id, $user->email, $user->name, $user->username,
              $user->birthday_date);

to get the email address, I keep getting file_get_content errors. I also get errors saying it doesn't recognize the register_user function. I feel that I'm close!! Please help if possible, thanks!

I'm not familiar with the library you're using that has register_user, but is there any reason you aren't using the Facebook connect PHP SDK? You can download it at http://github.com/facebook/php-sdk and it has examples for both obtaining the auth token with extended permissions and then pulling all the user information you seem to want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM