授权后如何重定向到canvas页面

发布于 2025-01-08 02:34:03 字数 1827 浏览 5 评论 0原文

我正在 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>

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2025-01-15 02:34:03

无法看到您在哪里请求权限,但为了在安装后将用户重定向到同一个应用程序,我使用了以下代码:

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

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'
            )
    );
墨小墨 2025-01-15 02:34:03

正如 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');
  }

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');
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文