Spilot
Spilot

Reputation: 1525

Can't get user id from facebook signedRequest in PHP .

On my index page I initialize the javascript sdk and go through the login/authorization flow. I then use AJAX to pass the signed request to my php page to be parsed. The code for parsing the signed request is copied directly from the documentation, I haven't changed anything. At the end of that code I try to get the user info that is returned in the same object as the signed request, but when I try to log any of those variables in the AJAX success callback, they come up 'undefined'.

//HTML

function onCheckLoginStatus (response)
    {
     if (response.status != "connected")
     {
      //redirect to login page;
     }
     else
     {

     //CONNECTED, Get signed request from response object and pass it to PHP page via AJAX

      $.ajax({
        url : "http://XXXXXXX/bn/signedRequest.php",
        type : 'POST',
        data: {signed_request: response.authResponse.signedRequest},
        success : function (result) {

           console.log("success"); 

           //THIS IS COMING UP UNDEFINED 

                console.log(result.uID);
        },
        error : function () {
           alert("error");
        }
    });

//PHP    
<?php

define('FACEBOOK_APP_ID', '27XXXXXX0&'); // Place your App Id here
define('FACEBOOK_SECRET', '8ea907XXXXXXX9e958'); // Place your App Secret Here

//GET THE SIGNED REQUEST

$signed_request = $_REQUEST['signed_request']; 



function parse_signed_request($signed_request, $secret) 
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}


// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) 
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) 
{
return base64_decode(strtr($input, '-_', '+/'));
}

if ($_REQUEST) 
{
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
}


$uID = $response["user_id"];
$name = $response["registration"]["name"]; 
$city = $response["registration"]["location"]["name"];

echo json_encode($uID);



?>

Upvotes: 0

Views: 396

Answers (1)

phwd
phwd

Reputation: 19995

Either send the entire $response

or

change the echo line to

echo($uID);

and change console.log line in the AJAX call to

console.log(result);

You can verify for yourself by just logging the result

 $.ajax({
        url : "http://XXXXXXX/bn/signedRequest.php",
        type : 'POST',
        data: {signed_request: response.authResponse.signedRequest},
        success : function (result) {

           console.log("success"); 

           //THIS IS COMING UP UNDEFINED 

           console.log(result); // <----- Check whether result is undefined
        },
        error : function () {
           alert("error");
        }
    });

Upvotes: 1

Related Questions