In the facebook canvas application, where can I send the signed request parameter as soon as I captured it in the log

I am using javascript sdk. The documentation says that you get a signed request from the response object, which returns FB.getLoginStatus () when the user status = connected, but now I need to parse the signed request. How to send it to php page Do I have parsing code? Do I include php code on the canvas application index page and then send signedRequest to the code on the same page? Or save the code on separate pages and pass SR.

The first block of code is on my index.html page. It checks the login status and receives the signed request parameter from the response object.

The second block is php code. facebook provides parsing of the signed request when it is captured through the registration plugin, but the plugin automatically sends SR to this page when you specify your URL as a parameter. In a canvas application, I have to pass it myself. How can I do it?

Javascript

FB.getLoginStatus(function(response) { if (response.status === 'connected') { // the user is logged in and has authenticated your // app, and response.authResponse supplies // the user ID, a valid access token, a signed // request, and the time the access token // and signed request each expire var uid = response.authResponse.userID; var accessToken = response.authResponse.accessToken; var signedRequest = response.authResponse.signedRequest; } else if (response.status === 'not_authorized') { // the user is logged in to Facebook, } else { // the user isn't logged in to Facebook. } }); 

PHP page

 <?php define('FACEBOOK_APP_ID', '3*****88&'); // Place your App Id here define('FACEBOOK_SECRET', '1345*****eb4f2da'); // Place your App Secret Here // No need to change the function body 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); } $name = $response["registration"]["name"]; $email = $response["registration"]["email"]; $password = $response["registration"]["password"]; $uID = $response["user_id"]; ?> 
+4
source share
2 answers

Turning around to Answer clay , you can implement it something like this: jQuery.get () :

 var signed_request = getSignedRequest(), // however you do this.. url = "http://yoursite.com/phppage.php", // wherever your php code is ajax = $.get(url, {signed_request: signed_request}); // initiate ajax call // ajax success handler ajax.done(function(ajaxResponse){ console.log(ajaxResponse); }); 

Of course you need to get this value in your PHP file

 $signed_request = $_GET['signed_request']; 
+4
source

Submit http://yoursite.com/phppage.php?signed_request= {signed_request_from_js}

You can do this with jQuery.get ()

+3
source

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


All Articles