2011-10-02 3 views
1

내 서버로 ajax 호출을하고 로그인 수를 업데이트하기 위해 auth.login에 등록했습니다. 는 PHP SDK를 같은 작업을 단호하게 사용자가 제대로 로그인되어 있는지 거부하고 나던Facebook OAUTH2 및 PHP/JSDK

JS 코드를 :.

window.fbAsyncInit = function() { 
    var channelurl='http://'+window.location.hostname+'/channel.html'; 
     FB.init({ 
      appId : window.appid, 
      status: true, 
      cookie: true, 
      xfbml: true, 
      channelURL : channelurl, // channel.html file 
      oauth : true // enable OAuth 2.0 
     }); 
     FB.Event.subscribe('auth.login', function (response) { 

        $("#fbconnecttext").html("<a>Logging in...</v>"); 

        $.ajax({ 
         url: "fbupdatelogincount", 
         type: "GET", 
         cache: false, 
         success: function (html) { 
          window.setTimeout('$("#fbconnecttext").html("")', 10000); 
          var rec = JSON.parse(html); 
          window.numlogins = rec["numlogins"]; 
          FB.api('/me', function (response) { 
          if (window.numlogins > 1) { 
           $("#fbconnecttext").html(window.welcomebacktext.replace("%s", response.first_name)); 
           $("#fbadminimg").attr("src", "common-images/smiley"); 
          } 
          else { 
           alert(window.firstlogintext.replace("%s", response.first_name)); 
          } 

          }); 

         } 
         }); 
       }); 

     FB.Event.subscribe('auth.logout', function (response) { 
        $("#fbconnecttext").html(window.fbconnecttext); 
        $("#fb-like").show(); 
        FB.XFBML.parse($('#fb-like').is()); 
       }); 
     FB.Event.subscribe('auth.sessionChange', function (response) {}); 
    }; 

(function(d){ 
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "http://connect.facebook.net/en_US/all.js"; 
    d.getElementsByTagName('head')[0].appendChild(js); 
    }(document)); 
} 

는 PHP 페이지

require_once("utils.php"); 
logText("In fbUpdatelogincount"); 
if(fbAuthentication()){ 
    logText("authenticated in fbUpdatelogincount"); 
    $r=fbUpdateStats(); 
    if(isset($_REQUEST["field"])) 
     echo $r[$_REQUEST["field"]]; 
    else 
     echo json_encode($r); 
} 
echo ""; 

그리고 마지막으로 fbAutentication 코드

:

function fbAuthentication(){ 

    global $facebook; 
    $facebook = new Facebook(array(
        'appId' => getSetting("fb:app_id"), 
        'secret' => getSetting("fb:secret") 
        )); 
    if($facebook->getUser()){ 
     try { 
      global $fb_isadmin,$fb_me; 
      $fb_me = $facebook->api('/me'); 
      $fb_isadmin=strstr(getSetting("fb:admins"),$facebook->getUser())!=false; 
      fbUpdateStats(); 
      return true; 
     } catch (FacebookApiException $e) { 
      /* exception handling todo */ 
     } 
     return true; 
    }else logText("No Valid user"); 

    return false; 

} 

주요 문제는 URL의 fbupdatelogincount을 발사 아약스 호출하지만, PHP 측에서 "아무도 로그인하지 않았습니다"라고 말합니다. 어떤 아이디어? 동일한 설치가 3.1.1 이전에는 정상적으로 작동했습니다.

답변

1

아무데도 설명되어 있지 않지만 응용 프로그램 암호를 auth.login 이벤트로 전달하면 성공적으로 실행됩니다.

이 시도 :

FB.Event.subscribe('auth.login', function(response) { 
    // callback 
}, { secret:'<?php print $facebook->getApiSecret(); ?>' }); 

원래 문제는 이후 페이스 북이 수정되었습니다.

+0

몇 가지 질문 :이 문서화되지 않은 동작으로 인해 정확히 무엇이 수정됩니까? 다른 사람들이이 수정 프로그램을 어떻게 사용합니까? 그리고 이것은 거대한 보안 구멍을 만들지 않을까요? FB AppId와 FB AppSecret을 공개적으로 공개하지 않으시겠습니까? – BuddyJoe

+0

Facebook 응용 프로그램 설정 아래의 옵션 인 "고급 응용 프로그램 비밀 필요 auth.login"이라는 레이블이있는 고급 탭에서 처음 시도해 볼 아이디어가 있습니다. 당시에는 auth.login 이벤트가 발생하여 사용자의 Facebook 세션이 제대로로드 될 수있는 유일한 수정 사항이었습니다. 이 시점에서 그 옵션은 App Settings에서 삭제 된 것으로 보이고 원래의 문제는 Facebook에 의해 수정되었습니다. 개발자에게 많은 통보없이 끊임없이 변경되는 API 작업의 성격입니다. –

+0

와우. 나는 OAuth 2.0과 페이스 북과 함께 지내고있다. auth.login을 강제 실행하면 좋은 해결책이었을 것입니다. 팝업이 나타나지만 이미 로그인 한 경우 이벤트가 발생하지 않는 방식으로 작동합니다. 내가 아는 바로는. – BuddyJoe

0

마지막으로 또 다른 해결책을 찾았습니다. 아약스 콜백에서 나는 accesstoken을 GET 매개 변수로 추가하고 PHP url에서 디코딩 한 다음 $ facebook-> setAccessToken ($ at)을 호출했습니다. 이제 잘 작동합니다. 따라서 새로운 SDK가 함께 작동하는 버그입니다. 무슨 날 ...;) -

FB.Event.subscribe('auth.authResponseChange', function (response) { 
    FB.XFBML.parse(document.getElementById('fb-like')); 
    if (response.authResponse) { 
    FB.api('/me', function(response) { 
      // $("#fbconnecttext").html('<a class="fbUserName">'+response.first_name+'</a>'); 
      }); 
     $.ajax({ 
      url: "fbupdatelogincount?accesstoken="+response.authResponse.accessToken, 
      type: "GET", 
      success: function (html) { 
       if (html) { 
      var rec = JSON.parse(html); 
        window.numlogins = rec["numlogins"]; 
        FB.api('/me', function (response) { 
         if (window.numlogins > 1) { 
          $("#fbconnecttext").html(window.welcomebacktext.replace("%s", response.first_name)); 
          $("#fbadminimg").attr("src", "common-images/smiley"); 
         } 
         else { 
          alert(window.firstlogintext.replace("%s", response.first_name)); 
         } 

        }); 
       } 

      } 
     }); 
    }else{ 
    //logged out 
    } 
});