admin管理员组

文章数量:1400134

I've been looking quite a lot for an actual example of how to implement asking for permissions with facebook login, but I couldn't find any. All I can find is the name of the permissions, advice on what to ask, but no examples.

The question is: Where and how can I ask for permissions using php or js? A bit of code would be very helpful. I am new to facebook developing and I've started reading all I can about facebook login and facebook api and trying to do a couple of small apps so I get used to how things work, but I am a bit stuck.


Edit I found this code, which seems to be what I've been looking for:

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

I've been looking quite a lot for an actual example of how to implement asking for permissions with facebook login, but I couldn't find any. All I can find is the name of the permissions, advice on what to ask, but no examples.

The question is: Where and how can I ask for permissions using php or js? A bit of code would be very helpful. I am new to facebook developing and I've started reading all I can about facebook login and facebook api and trying to do a couple of small apps so I get used to how things work, but I am a bit stuck.


Edit I found this code, which seems to be what I've been looking for:

FB.login(function(response) {
  // handle the response
}, {scope: 'email,publish_actions'})
Share Improve this question edited Mar 7, 2013 at 21:52 MihaiB asked Mar 7, 2013 at 21:17 MihaiBMihaiB 1391 gold badge4 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You should start with Facebook PHP SDK , the key is to understand server-side login flow, as explained here, you can use this example as a start:

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

本文标签: phpFacebook request permissions exampleStack Overflow