Facebook重定向具有扩展权限

时间:2010-08-23 19:05:05

标签: facebook

我正在尝试基于本教程制作facebook connect: http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

他们已经给你一些脚本来解决它,这是扩展权限一:

<?php
# We require the library
require("facebook.php");

# Creating the facebook object
$facebook = new Facebook(array(
 'appId'  => '...',
 'secret' => '...',
 'cookie' => true
));

# Let's see if we have an active session
$session = $facebook->getSession();

if(!empty($session)) {
 # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
 try{
  $uid = $facebook->getUser();

  # req_perms is a comma separated list of the permissions needed
  $url = $facebook->getLoginUrl(array(
   'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
  ));
  header("Location: {$url} ");
 } catch (Exception $e){}
} else {
 # There's no active session, let's generate one
 $login_url = $facebook->getLoginUrl();
 header("Location: ".$login_url);
}

当我执行它时,它允许我允许扩展权限,但后来我得到一个重定向循环错误。当我在Facebook登记时,它已授予扩展权限。

现在我尝试将它实现到他们提供的登录脚本中,这就是

# We require the library
require("facebook.php");

# Creating the facebook object
$facebook = new Facebook(array(
 'appId'  => '...',
 'secret' => '...',
 'cookie' => true
));

# Let's see if we have an active session
$session = $facebook->getSession();

if(!empty($session)) {
 # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
 try{
  $uid = $facebook->getUser();
  $user = $facebook->api('/me');

 } catch (Exception $e){}

 if(!empty($user)){
  # We have an active session, let's check if we have already registered the user
  $query = mysql_query("SELECT * FROM users WHERE oauth_prov = 1 AND oauth_id = ". $user['id']);
  $result = mysql_fetch_array($query);

  # If not, let's add it to the database

  echo $user['id'] . $user['name'];

  if(empty($result)){
   $query = mysql_query("INSERT INTO users (oauth_prov, oauth_id, username) VALUES ('1', {$user['id']}, '{$user['name']}')");
   $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
   $result = mysql_fetch_array($query);
  }

  // this sets variables in the session 
  $_SESSION['id'] = $result['id'];
  $_SESSION['oauth_uid'] = $result['oauth_id'];
  $_SESSION['oauth_provider'] = $result['oauth_provider'];
  $_SESSION['username'] = $result['username'];



 } else {
  # For testing purposes, if there was an error, let's kill the script
  die("There was an error.");
 }
} else {
 # There's no active session, let's generate one
 $login_url = $facebook->getLoginUrl();
 header("Location: ".$login_url);
}

但是,我不知道它应该去哪里,因为我想在登录的同时请求权限以获取用户详细信息。如果我这样说:

try{
  $uid = $facebook->getUser();

  # req_perms is a comma separated list of the permissions needed
  $url = $facebook->getLoginUrl(array(
   'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
  ));
  header("Location: {$url} ");


 } catch (Exception $e){}

在我设置会话值后,我甚至不允许任何东西,它只是挂起,我得到一个重定向错误。 如果我在获得facebook user_id后立即将它放入,那么它会给我一个允许对话框(虽然一个接一个地显然要求两次,我想要停止的东西)但是然后再次给我一个重定向错误,授予了扩展权限

我真的不知道把它放在哪里,facebook的文档非常糟糕

1 个答案:

答案 0 :(得分:5)

而不是:

# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array('req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));

尝试:

$url = $facebook->getLoginUrl(array('scope' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'));

有关完整示例,请参阅php-sdk: https://github.com/facebook/php-sdk/blob/master/examples/example.php