簡體   English   中英

使用Facebook的PHP SDK檢索電子郵件

[英]Use facebook's php SDK to retrieve email

到目前為止,我已經設置它來檢索基本的用戶信息,沒有任何擴展。 我試圖也獲取用戶電子郵件,但沒有成功。 這是我得到的:

   $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不會顯示擴展的數據,例如電子郵件,僅是基本數據。 如何請求啟用了更多權限的acces令牌?

您需要通過以下其中一種方式請求擴展權限 ,即“電子郵件”:

  1. PHP SDK

    使用sdk對象的getLoginUrl()方法,並在“作用域”中指定所需的擴展權限,並將用戶重定向到其返回值,此外,還需要在facebook應用程序的管理頁面中添加域和站點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()函數在第二個參數中詢問您所需的擴展權限。

有關整個身份驗證過程的詳細信息,請參閱此頁面

如果要檢查當前用戶是否具有所需的所有權限,可以使用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()

在我這邊,我這樣做:

// 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中的SCOPE,您可以在以下位置查看開發人員網站上的所有不同權限:

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

祝好運

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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM