4

Facebook의 Javascript SDK "FB.ui"를 사용하여 OAuth 대화 상자를 표시합니다. 허용을 클릭하면 세션 객체를 캡처하고 사용자 ID를 추출한 다음 스크립트에서 추가로 사용해야합니다. 어떤 이유로 세션이 존재하더라도 제대로 작동하지 않을 수 있습니다.Facebook 자바 스크립트 SDK 세션 객체에서 사용자 ID 추출하기

<script src="http://connect.facebook.net/en_US/all.js"></script> 
<div id="fb-root"></div> 
<script type="text/javascript"> 
FB.init({ 
    appId : '***************', 
    status : true, 
    cookie : true, 
    xfbml : true 
}); 
FB.getLoginStatus(function(response) { 
    if (response.session) { 
     //do something 
    } else { 
     FB.ui({ 
      method: 'oauth', 
      display: 'page', 
      scope: 'email', 
      perms: 'email' 
     }, 
     function(response) { 
      alert(response.session.uid); //returns underfined 
     }); 
    } 
}); 
</script> 

답변

0

내가 원하는대로 작동하도록했습니다. 이것은 최선의 방법은 아니지만, 주위를 많이 파고 좌절감을 겪은 후에는 동일한 질문을 한 사람이 올바른 길을 걷는 데 도움이되기를 바랍니다.

JS 소스 :

FB.getLoginStatus(function(response) { 
    if (!response.session) { 
     //initiate FB OAuth js popup dialog 
     FB.ui({ 
      method: 'oauth', 
      display: 'page', 
      scope: 'email', 
      perms: 'email' 
     }, 
     function(response) { 
      if (response.session) { //if permission Allowed 
       var thesession = response.session; 
       var thesession = eval('(' + thesession + ')'); //decode json 
       //POSTing to local file get.user_graph.php, to fetch user info 
       $.ajax({ 
        type: "POST", 
        url: "get.user_graph.php", 
        data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid, 
        dataType: "text", 
        success: function(user_graph){ 
         var user_graph1 = eval('('+user_graph+')'); 
         alert(user_graph1.name); //users name 
         alert(user_graph1.id); //users id 
         alert(user_graph1.email); //users email 
         alert(user_graph1.link); //users profile link 
        } 
       }); 
      } else { 
       //if permission Not Allowed 
      } 
     }); 
    } 
}); 

get.user_graph.php 소스 :

//exchange our session for an access_token 
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions'); 
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred'); 
$curl_token = curl_init(POSTURL); 
curl_setopt($curl_token, CURLOPT_POST,1); 
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS); 
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($curl_token, CURLOPT_HEADER,0); 
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1); 
$token = curl_exec($curl_token); 

$token_decoded = json_decode($token,true); 
//get the user graph (personal info) 
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']); 
echo $user_graph; 
1

페이스 북의 로그인 버튼에서 로그인하면 고유 한 액세스 토큰을 포함하는 쿠키가 시스템에 저장됩니다. 그 쿠키에는 페이스 북의 ID도 들어 있습니다.

0

응답을 확인하십시오. 나는 그것이 "display" must be one of "popup", "iframe" or "hidden".과 같은 것을 말할 것입니다. oauth 대화 상자는 기본 "페이지"디스플레이로 호출 할 수 없습니다. display: 'iframe'은 access_token을 포함해야하므로, oauth 대화 상자를 처음에 호출하는 이유는> : l입니다.

대부분의 사람들이 요즘 차단 한 팝업을 사용하고 싶지 않으면 FB.ui를 사용하여 oauth 대화 상자를 사용할 수 있다고 생각하지 않습니다.

+0

이봐, 당신 덕분에 응답! 나는 이것이 내가 필요로하는 방식으로 작동하도록했다. 아래에서 내 대답을 확인해 보라. – Yev

2

는 사용자 ID를 얻으려면 :

FB.getLoginStatus(function(response) { 
    alert(response.authResponse.userID); 
});