2011-04-13 147 views
1

我試圖在我的網站上實現FB.login()。我得到'請求許可'窗口,一旦用戶接受它,我想將他重定向到我的自定義頁面(在同一個域下)。我在哪裏以及如何在代碼中設置它?Fb.login後的Facebook重定向

<script> 
     //initializing API 
     window.fbAsyncInit = function() { 
      FB.init({ appId: 'xxx', status: true, cookie: true, 
       xfbml: true 
      }); 
     }; 
     (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); 
     }()); 

    function fblogin() { 
     FB.login(function (response) { 
      //... 
     }, { perms: 'user_birthday,user_location' }); 
    } 
</script> 

回答

3

您應該訂閱auth.login事件,像:

window.fbAsyncInit = function() { 
    FB.init({ appId: 'xxx', status: true, cookie: true, 
     xfbml: true 
    }); 

    FB.Event.subscribe('auth.login', function(resp) { 
     window.location = 'http://www.domain.com/custom_page.php'; 
    }); 
}; 
+2

你也不妨訂閱auth.sessionChange誰擁有遊客已經授權您的應用程序並返回到原始頁面。 – CameraSchoolDropout 2011-04-14 22:05:29

+1

@CameraSchoolDropout:True,但我總是在PHP-SDK中使用它,所以我通過使用'$ fb-> getSession()'檢查會話來處理這些用戶。 +1 btw – ifaour 2011-04-15 07:00:42

+0

這不適用於Safari – Joe 2012-05-18 15:21:40

0

這個工作對我來說:

<div id='loginmsg'> 
<div class="fb-login-button" data-scope="email,publish_stream,read_stream" data-show-faces="true" data-width="720" data-max-rows="1"></div> 
</div> 


<script> 
FB.Event.subscribe('auth.login', function(resp) {   
    $('#loginmsg').html("<h2>...just a moment...</h2>"); 
    window.location = '/logged-in.html'; 
    }); 

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     var uid = response.authResponse.userID; 
     var accessToken = response.authResponse.accessToken; 
     $('#loginmsg').html("<h2>...just a moment...</h2>"); 
     window.location = '/logged-in.html'; 
    } 
    }); 

</script>