2012-09-27 39 views
0

我正在嘗試調用Facebook oauth登錄。一旦用facebook登錄頁面出現彈出窗口,它不會再重定向到同一個應用程序。Facebook oAuth不會重定向迴應用程序

<html> 
<head> 
    <script type="text/javascript" src="/mp/struts/js/base/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="/mp/maincommon/static/js/all.js"></script> 
    <script type="text/javascript"> 
     function onFaceBookAuth(user){ 
      var id = user['id']; 
      var firstName = user['first_name']; 
      var lastName = user['last_name']; 
      var email = user['email']; 

      var params = "applicantId="+id+"&firstName="+firstName+"&lastName="+lastName; 
      var xmlhttp; 
      if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } 
      else {// code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        var response = xmlhttp.responseText; 
        if(response.indexOf("new") != -1){ 
         $('#applicantId').val(id); 
         $('#fbfirstName').val(firstName); 
         $('#fblastName').val(lastName); 
         $('#fbemail').val(email); 
         $('#applicantRegisterForm').submit(); 
        } 
        else{ 
         window.location = 'applicantHome.jsp?applicantId='+id; 
        } 
       } 
     }; 
     xmlhttp.open("POST", "login?mode=2", true); 
     xmlhttp.setRequestHeader("Content-type", 
       "application/x-www-form-urlencoded"); 
     xmlhttp.send(params); 
    } 

    function fbAsyncInit(){ 
     FB.init({appId: '307292312637177', status: true, cookie: true, xfbml: true}); 
     FB.getLoginStatus(function(response) { 
      var stringArr=JSON.stringify(response); 
       if (response.status == 'connected') { 
        fbLoginStatus = true; 
        showLogoutText(); 
       } 
       else if (response.status == 'not_authorized') { 
        fbLoginStatus = false; 
       } 
       else { 
       // the user isn't even logged in to Facebook. 
        fbLoginStatus = false; 
       } 
      }); 
     FB.Event.subscribe('auth.login', function(response) { 
      FB.api('/me', function(user) { 
       if (user) { 
        if(user.id!=undefined){ 
        onFaceBookAuth(user); 
        } 
       } 
       }); 
     }); 
     FB.Event.subscribe('auth.logout', function(response) { 
     }); 

     FB.api('/me', function(user) { 
      if (user) { 
       if(user.id!=undefined){ 
       onFaceBookAuth(user); 
       } 
      } 
      }); 
    }; 

    </script> 
</head> 
<body> 
    <div id="facebookLogin" class="fb-login-button" onclick="fbAsyncInit()" data-scope="email"> 
          Login with Facebook</div> 
</body> 
</html> 

彈出即將進入Facebook的憑據。但是,之後它不會重定向到同一個應用程序。

這個問題與「appId」有關嗎?因爲我給了隨機數,我不知道應該給「appId」什麼。如何爲我的應用程序獲取appId?

在此先感謝

回答

0

是的,AppId很重要。該的AppId您的應用程序可以從Facebook的開發者頁面:https://developers.facebook.com/apps

您需要註冊一個應用程序,然後才能使用Facebook的API進行交互,並且爲了OAuth的正確重定向,則需要與供應它指向您的網站的路徑,您可以在上面的url中找到詳細信息表單。

還有更多關於在根網址上爲Facebook開發的信息:https://developers.facebook.com/

+0

我在本地測試。目前它不是活的。那麼,localhost的url註冊會起作用嗎? – user1673257

+0

否。要測試oAuth集成,您需要使用可公開訪問的網址。 – JcFx

+1

@JFFx,這是不正確的。 Auth流程的所有相關部分發生在您的瀏覽器內,並且您的瀏覽器知道哪臺機器是本地主機。使用當前僅在本地主機上運行的應用進行身份驗證非常好,我一直在本地開發時一直這樣做。 – CBroe

相關問題