PHP SDK - 在询问用户权限后重定向问题

时间:2011-08-10 09:18:01

标签: facebook facebook-php-sdk

我的应用程序位于Facebook粉丝页面的标签中。

当用户访问此页面的标签时,我向他请求了一些权限。

我的问题是,当用户接受权限时,他会被重定向到http://apps.facebook.com/而不是页面的标签。

$my_url = "https://apps.facebook.com/<my_app_name>/";

如果我将my_url值更改为http://www.facebook.com/pages/,则用户处于无限循环状态。

我的代码:

 if (isset($_REQUEST["code"])) {
  $code = $_REQUEST["code"];
 }
 if(empty($code)) {
  // Get permission from the user to manage their Page.
  $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=read_stream";
  echo('<script>top.location.href="' . $dialog_url . '";</script>');
 } else {
 //my code
 }

感谢。

1 个答案:

答案 0 :(得分:0)

这是因为code变量永远不会通过粉丝页面/应用程序选项卡URL传递到您的iFrame选项卡的内容(如果您使用应用程序选项卡URL,则不会将其他参数传递给iFrame URL) 。因此,按照上面的代码示例,JavaScript重定向到身份验证对话框将始终传递给客户端浏览器。

您应该使用最新的PHP SDK(v3.1.1)和JavaScript SDK来进行身份验证: http://developers.facebook.com/blog/post/534/

以上博文中的代码示例:     

require 'php-sdk/src/facebook.php';

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

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