2012-07-17 158 views
0

到目前为止,我已设置检索基本的用户信息,没有任何扩展。我也试图获得用户的电子邮件,没有成功。这是我得到的:使用Facebook的PHP SDK来检索电子邮件

$facebook = new Facebook(array(
      'appId' => APP_ID, 
      'secret' => APP_SECRET, 
      'cookie' => true, 

     )); 

$session = $facebook->getSession(); 

if (!empty($session)) { 
    try { 
    $params = array(
     'scope' => 'email', 
     'redirect_uri' => 'https://www.dormduels/splash_test' 
    ); 

    $loginUrl = $facebook->getLoginUrl($params); 
     $uid = $facebook->getUser(); 

     $access_token = $facebook->getAccessToken(); 

     echo $access_token; 
     $user = $facebook->api('me?fields=name,first_name,last_name,username,email&access_token='.$access_token); 
     echo "<pre>"; 
     print_r($user); 
     exit(); 

问题是。在Facebook图形浏览器中测试时,它显示的getAccessToken不显示扩展数据,如电子邮件,只有基本的。我如何请求启用更多权限的访问令牌?

回答

1

您需要通过以下方式之一来这里问extended permissions,即 “电子邮件” 为:

  1. PHP SDK

    使用SDK对象的getLoginUrl()方法,并指定在'范围'中需要扩展的权限,并将用户重定向到它的返回值,还需要在您的facebook app's admin page中添加域和站点URL。这里有一个例子:

    define('APP_ID', 'TODO FILL IT WITH YOURS'); 
    define('APP_SECRET', 'TODO FILL IT WITH YOURS'); 
    
    require_once 'facebook.php'; 
    
    function redirect_to_login($fb){ 
        $login_url = $fb->getLoginUrl(array(
         'scope' => 'email', 
        )); 
        header('Location: '.$login_url); 
        exit; 
    } 
    
    $fb = new Facebook(array(
        'appId' => APP_ID, 
        'secret' => APP_SECRET, 
        'cookie' => true, 
    )); 
    
    $uid = $fb->getUser(); 
    if (!$uid || !check_perms($fb, $uid, array('email'))) { // see check_perms below 
        // send users to login/auth page if they are not logged in, 
        // or not installed the app, 
        // or don't have all the perms we need 
        redirect_to_login($fb); 
    } 
    
    try { 
        $me = $fb->api('/me'); 
        var_dump($me); 
    } catch (Exception $e) { 
        redirect_to_login($fb); 
    } 
    
  2. JS SDK

    使用FB.login()功能要求您需要的扩展权限,在第二个参数。

有关整个认证过程的详细信息,请参阅this page

如果要检查当前用户拥有你想要的所有权限,可以使用FQL这样的:

function check_perms($facebook, $uid, $perms = array()) { 

    $perms_str = join(' = 1 and ', $perms).' = 1'; 
    $query = "select uid from permissions where uid = '".$uid."' and ".$perms_str; 

    try { 
     $row = $facebook->api(array(
      'method' => 'fql.query', 
      'query' => $query, 
     )); 
     return $row && $row[0]['uid'] == $uid; 
    } catch (Exception $e) { 
     return false; 
    } 
} 

注:php sdk版本可能已过时,那么的getSession( )方法都赞成的getUser()/ getAccessToken()

+0

更新我的问题与getLoginUrl没有运气 – 2012-07-17 19:50:49

+0

我认为你可能会把它放在一个错误的地方(这个例子在这种情况下是一个语法错误),你应该把getLoginUrl()调用放在'if($ facebook-> getUser )){...',也只是调用它不会将用户重定向到t他的网址返回。你需要发送一个http头或其他方式来发送浏览器,比如'header(“Location:”。$ loginUrl);' – complex857 2012-07-17 19:58:15

+0

已更新为新的sdk,你是对的我已经过时了,并且将getloginurl移入了if用户,但仍...没有...没有权限的访问令牌正在givin – 2012-07-17 20:12:12

1

在我身边的被否决,我做这种方式:

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => FB_API_KEY, 
    'secret' => FB_API_SECRET, 
)); 

// Get User ID 
$fb_user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $fb_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 ($fb_user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $fb_user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $fb_user = null; 
    } 
} 

// Login or logout url will be needed depending on current user state. 
if ($fb_user) { 
    $fb_loggedin = true; 
} else { 
    $fb_loggedin = false; 
    $fb_loginUrl = $facebook->getLoginUrl(array(
     'scope' => 'email,user_location', 
     'redirect_uri' => 'http://'.$_SERVER['HTTP_HOST'].'/debut_'.($lang == 'fr' ? 'fr' : 'eng').'.php' 
    )); 
} 

在getLoginUrl范围要看看,你可以看到开发人员网站上的所有不同的权限:

https://developers.facebook.com/docs/authentication/permissions/

好运

+0

但不应该在新的Facebook下定义范围? – 2012-07-17 20:16:41

+0

从来没有在那里定义它,但我也可能做得不对,我只是告诉你我的方式,它的工作原理... – 2012-07-17 20:29:47

+0

但是,只有当用户未登录时才会调用getLoginUrl。我有一个新的Facebook/getuser,提示用户登录 – 2012-07-17 20:31:35

-1
Try following: 
$facebook = new Facebook(array(
    'appId' => APP_ID, 
    'secret' => APP_SECRET, 
    'cookie' => true, 
)); 

public function login() { 
     $loginUrl= $facebook->getLoginUrl(array('scope' => 'email', 
        'redirect_uri' => 'your redirectUri', 
        'display' => 'popup' 
       )); 
     header("Location:$loginUrl"); 
    } 
public function fUserInfo() { 
     $access_token = $facebook->getAccessToken(); 
     //check permissions list 
     $permissions_list = $facebook->api('/me/permissions', 'GET', array('access_token' => $access_token)); 
     $permissions_needed = array('email'); 
     foreach ($permissions_needed as $perm) { 
      if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) { 
       login(); 
      } 
     } 
     $user = $facebook->getUser(); 
     if ($user) { 
      try { 
       $userInfo = $facebook->api("/$user"); 
      } catch (FacebookApiException $e) { 
       Config::getLogger()->debug($e->getMessage()); 
      } 
     } 
     print_r($userInfo); 
    } 
相关问题