AJAX 与 Facebook 身份验证

2024-05-02

我已经构建了一个完全基于 AJAX 的应用程序,它没有页面刷新并使用 AJAX 加载所有内容。现在我想以一种不会重定向用户进行页面刷新的方式嵌入 Facebook 身份验证。

目前 Facebook 的工作方式如下:

用户通过单击 Facebook Connect 按钮被重定向到 Facebook

它将在用户操作后“允许”或“禁止”重定向到http://www.xxxxxxxxx.com/JoinFB.html http://www.xxxxxxxxxx.com/JoinFB.html然后我可以使用 Javascript 读取用户信息。

问题是我可以使用 IFRAME 或其他东西以某种方式克服这个页面重定向过程吗?

如何检测 IFRAME 用户是否允许?


我会用JS-SDK https://developers.facebook.com/docs/reference/javascript/,一个完美的例子是Facebook PHP-SDK 示例 https://github.com/facebook/facebook-php-sdk/blob/master/examples/with_js_sdk.php本身:

<?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 '<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 { ?>
      <fb:login-button></fb:login-button>
    <?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>

现在单击fb:login-button:

<fb:login-button scope="offline_access,publish_stream,sms,email,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins"></fb:login-button>

将打开 Facebook 弹出窗口,授权过程后弹出窗口将自动关闭,并且auth.login事件将被触发,我会更改重新加载方法window.location.reload();到 ajax 调用来做你正在做的事情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AJAX 与 Facebook 身份验证 的相关文章

随机推荐