AJAX与Facebook的身份验证 [英] AJAX with Facebook Authentication

查看:120
本文介绍了AJAX与Facebook的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个完全基于AJAX的应用程序,它并没有刷新页面,并使用AJAX加载的一切。现在,我想在某种程度上,它不会将用户重定向到使页面刷新嵌入Facebook的身份验证。

I have built an completely AJAX based application which has no page refresh and load everything using AJAX. Now I want to embed facebook authentication in a way it won't redirect the user to make page refresh.

目前Facebook的工作是这样的:

Currently the facebook works this way:

用户被重定向至Facebook点击Facebook的连接按钮<一href="https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxx&redirect_uri=http://www.xxxxxxxxxx.com/JoinFB.html?&type=user_agent&display=popup&scope=offline_access,publish_stream,sms,email,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins" rel="nofollow">https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxx&redirect_uri=http://www.xxxxxxxxxx.com/JoinFB.html?&type=user_agent&display=popup&scope=offline_access,publish_stream,sms,email,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins

User is being redirected to Facebook by clicking Facebook Connect button https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxxxxxxxx&redirect_uri=http://www.xxxxxxxxxx.com/JoinFB.html?&type=user_agent&display=popup&scope=offline_access,publish_stream,sms,email,user_birthday,user_photos,user_photo_video_tags,user_checkins,friends_checkins

它将用户的行为是否允许或不允许重定向到 HTTP后://www.xxxxxxxxxx .COM / JoinFB.html ,然后我就可以使用JavaScript来读取用户信息。

It will after user action whether "Allow" or "Disallow" redirect to http://www.xxxxxxxxxx.com/JoinFB.html and I can then use Javascript to read user information.

问题是,我可以克服这个页面重定向过程中以某种方式使用iframe或什么吗?

The problem is that can I overcome this page redirection process somehow using IFRAME or anything?

我将如何检测IFRAME为用户允许或不?

How will I detect IFRAME as user allowed or not?

推荐答案

我会使用 JS -SDK ,一个很好的例子就是的Facebook PHP-SDK例如本身:

I would use the JS-SDK, a perfect example is the Facebook PHP-SDK example itself:

<?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:登录按钮

<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做任何你正在做的

Will open a facebook pop-up and after the authorization process the pop-up will close automatically and the auth.login event will be triggered, I would change the reload method window.location.reload(); to an ajax call to do whatever you are doing.

这篇关于AJAX与Facebook的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆