Question

I am using Facebook's Javascript SDK "FB.ui" to pull up an OAuth dialog. Once you click allow, I need to capture the session object, extract the user id and use it further in my script. For some reason I cannot get this working properly, I keep getting undefined, even though the session does exist.

<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>
Était-ce utile?

La solution 4

So I got this to work as I would like. This may not be the best way, but after much digging around and frustration I hope this could help somebody with the same question get on the right track.

JS source:

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 source:

//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;

Autres conseils

To get the userID:

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

When you login from login button of facebook then it stores cookies in your system that contains your access token that is also unique. That cookies contains your facebook id also.

Check your response. I bet it says something like "display" must be one of "popup", "iframe" or "hidden". Apparently the oauth dialog cannot be called with the default "page" display. And display: 'iframe' requires that you include an access_token, which is why you're calling the oauth dialog in the first place >:l .

I don't think FB.ui can currently be used to get the oauth dialog, unless you want to use a popup, which most everyone has blocked nowadays.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top