2012-07-17 37 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); 
    } 
相關問題