2011-01-25 223 views
2

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

目前Facebook的工作是这样的:

用户正在通过点击Facebook的连接按钮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

重定向到Facebook的这个预计会在用户活动是否“允许”或“禁止”重定向到http://www.xxxxxxxxxx.com/JoinFB.html可以和我然后使用Javascript来阅读用户信息。

问题是我可以用IFRAME或其他方法克服这个页面重定向过程吗?

我将如何检测IFRAME作为用户允许与否?

回答

4

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

<?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调用,以执行您正在做的任何操作。

+1

它会在iphone safari中打开一个弹出窗口吗? – Neutralizer 2011-01-26 11:55:59