简体   繁体   中英

facebook app doesn't reload after Request for Permission Popup - using PHP SDK (v.3.1.1)

My old facebook application requires updating to the latest php sdk as was pointed out in another question. My next problem is getting it to work with the latest php sdk 3.1.1

I'm using the example that comes with the latest php sdk ie; with_js_sdk.php

It works fine up until one gives permission to access the application but instead of reloading bringing the user to the application it returns the user to the log in again. How do I get the user redirected to the application component. Note I have an alert that it calls window.location.reload(); but it doesn't show the user after he added the application

The code is below:

<?php

require 'facebook.php';

$appCanvasPage = 'https://apps.facebook.com/myapp/';
$app_id = "";
$app_secret = "";


$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

echo "user=".$user."\n";


?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { ?>


    <h2> THIS IS MY APP </h2>    

    <?php } else { ?>

      <fb:login-button></fb:login-button>

    <?php } ?>

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>',
          cookie: true,
          xfbml: true,
          oauth: true
        });

        FB.Event.subscribe('auth.login', function(response) {
          alert(At reload);
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });

      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>

I found a simple solution without using the sdk which was on facebook.

the link is: https://developers.facebook.com/docs/appsonfacebook/tutorial/

<?php 

     $app_id = "YOUR_APP_ID";

     $canvas_page = "YOUR_CANVAS_PAGE_URL";

     $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page);

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
            echo ("Welcome User: " . $data["user_id"]);
     } 
?>

did the trick.

David j.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM