Retrieve user id from Facebook. Javascript SDK Session Object

I use the Facebook Javascript SDK "FB.ui" to open the OAuth dialog. As soon as 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 can't get this to work properly, I keep getting undefined, although 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> 
+4
source share
4 answers

So, I got it to work as I would like. This may not be the best way, but after long jerks and disappointments, I hope that this can help someone with the same question to take the right path.

Source 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 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; 
0
source

To get the user ID:

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

When you log in using the facebook login button, then it stores cookies on your system that contain your access token, which is also unique. This cookies also contain your facebook id.

+1
source

Check your answer. I bet it says something like "display" must be one of "popup", "iframe" or "hidden". Apparently, the oauth dialog cannot be called up with a standard "page" display. And display: 'iframe' requires you to enable access_token, so you call the oauth dialog first>>: l.

I do not think that FB.ui can currently be used to get the oauth dialog if you do not want to use the pop-up window that most have currently blocked.

0
source

Source: https://habr.com/ru/post/1344222/


All Articles