使用新的 PHP SDK (3.XX) 请求许可

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7189151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:13:33  来源:igfitidea点击:

Asking for permission using new PHP SDK (3.X.X)

phpfacebookpermissions

提问by King Julien

How can I ask for permissions using new PHP SDK? I don't want to use the graph api and parse the url all the time. When the application is opened it should automatically ask for permissions if the user hasn't granted one already.

如何使用新的 PHP SDK 请求权限?我不想一直使用图形 api 并解析 url。当应用程序打开时,如果用户尚未授予权限,它应该自动请求权限。

回答by Henrik Peinar

Here's how i'm doing it with the latest PHP SDK (3.0.1)

这是我如何使用最新的 PHP SDK (3.0.1)

// init new facebook class instance with app info (taken from the DB)
$facebook = new Facebook(array(
    'appId' => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET'
));
// get user UID
$fb_user_id = $facebook->getUser();

    // get the url where to redirect the user
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream, email'));

// check if we have valid user
if ($fb_user_id) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $fb_user_profile = $facebook->api('/me');   

    } catch (FacebookApiException $e) {
        $fb_user_id = NULL;
        // seems we don't have enough permissions
        // we use javascript to redirect user instead of header() due to Facebook bug
        print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

        // kill the code so nothing else will happen before user gives us permissions
        die();
    }

} else {
    // seems our user hasn't logged in, redirect him to a FB login page

    print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

    // kill the code so nothing else will happen before user gives us permissions
    die();
}

// at this point we have an logged in user who has given permissions to our APP
// basic user info can be fetched easily
print "Welcome to my app". $fb_user_profile['name'];

回答by Shawn E Carter

Session Based Login with scope and Logout with access_token for PHP-SDK 3.2.0.

PHP-SDK 3.2.0具有范围的基于会话的登录和具有 access_token 的注销。

<?php
require './src/facebook.php';
$facebook = new Facebook(array(
  'appId'  => '135669679827333',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
));
$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) {
    $user = null;
  }
}
if ($user) {
  $params = array(access_token => ''.$access_token.'');
  $logoutUrl = $facebook->getLogoutUrl($params);
} else {
  $params = array(
     scope => 'read_stream,publish_stream,publish_actions,read_friendlists',
     //redirect_uri => $url
  );
  $loginUrl = $facebook->getLoginUrl($params);
};
$access_token = $_SESSION['fb_135669679827333_access_token'];
?>

.

.

<?php if($_SESSION['fb_135669679827333_access_token']): ?>
  <a href="<?php echo $logoutUrl; ?>&access_token=<?php echo $access_token; ?>" target="_parent">Login & Connect</a>
<?php else: ?>
  <a href="<?php echo $loginUrl; ?>" target="_parent">Login & Connect</a>
<?php endif ?>