Facebook请求权限的例子


Facebook request permissions example?

我一直在寻找一个如何实现facebook登录请求权限的实际示例,但我找不到任何。我只能找到权限的名称,关于询问什么的建议,但没有例子。

问题是:我在哪里以及如何使用php或js请求权限?一点代码会很有帮助。我是facebook开发的新手,我已经开始阅读所有关于facebook登录和facebook api的资料,并尝试做一些小应用程序,以便我习惯它们的工作方式,但我有点卡住了。


编辑我发现了这个代码,这似乎是我一直在寻找的:

FB.login(function(response) {
  // handle the response
}, {scope: 'email,publish_actions'})

您应该从Facebook PHP SDK开始,关键是要理解服务器端登录流程,如这里所述,您可以使用以下示例作为开始:

require 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOU_APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array("scope" => "user_photos"));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title></title>
  </head>
  <body>    
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>
    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>
    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
  </body>
</html>