2011-01-25 16 views
2

私は完全にAJAXベースのアプリケーションを構築しました。このアプリケーションはページリフレッシュがなく、AJAXを使用してすべてをロードします。今私はページをリフレッシュするためにユーザーをリダイレクトしない方法でfacebook認証を埋め込みたいと思う。Facebook認証付きAJAX

現在、Facebookはこのように動作します:

ユーザーは、それがユーザーアクションの後に「許可」またはhttp://www.xxxxxxxxxx.com/JoinFB.htmlにリダイレクトする「不許可」と私ができるかどうかのFacebook Connectボタンを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のにリダイレクトされています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

+0

以前はモバイルデバイス用に開発したわけではありませんが、結果をコメントして返信することはできます。 ;-) – ifaour

+0

これはコードの抜粋がうまく機能します。本当にありがとう! – Wykk