How to redirect after authorizing to the canvas page

牧云@^-^@ 提交于 2019-12-11 17:59:58

问题


I am using this code from the example on Php-sdk , wondering how do I redirect the user to the canvas page after authorizing, currently it just sends back to the php page.

<?php
require 'src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'AppId',
  'secret' => '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;
  }
}

?>

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user) { 
    echo " authorized";
    ?>
      Your user profile is
      <pre>
        <?php print htmlspecialchars(print_r($user_profile, true)); ?>
      </pre>
    <?php } else { echo "Not Authorized"; ?>
      <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) {
          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>

So in short, how do I set my redirect URL?


回答1:


Can't see where you are asking for permissions, but to redirect users to the same app after installing i use this code:

$loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'user_about_me',
                'redirect_uri'  => 'FACEBOOKS_FULL_APP_URL'
            )
    );



回答2:


As moguzalp referred in the comments, this bit of code did it for me

<script type='text/javascript'>
if (window.top.location == window.location) 
window.top.location = 'https://apps.facebook.com/myapp';
</script>

I just added this under

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  }


来源:https://stackoverflow.com/questions/9399736/how-to-redirect-after-authorizing-to-the-canvas-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!