如何在授权到画布页面后重定向

时间:2012-02-22 17:18:30

标签: facebook redirect facebook-apps facebook-authentication

我正在使用Php-sdk示例中的代码,想知道如何在授权后将用户重定向到画布页面,目前它只是发送回php页面。

<?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>

简而言之,如何设置重定向网址?

2 个答案:

答案 0 :(得分:1)

无法查看您要求权限的位置,但在安装后将用户重定向到同一个应用程序我使用此代码:

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

答案 1 :(得分:0)

正如moguzalp在评论中所提到的,这段代码为我做了

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

我刚刚在

下添加了这个
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  }
相关问题