到目前为止,我已经将其设置为检索基本用户信息,没有任何扩展。我也在尝试获取用户电子邮件,但没有成功。这是我得到的:
$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();
问题是,当在facebooks图形资源管理器中测试时,它显示的getAccessToken不会显示像电子邮件这样的扩展数据,只显示基本数据。如何申请更多权限启用的acces令牌?
在我这边,我是这样做的:
// 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/
祝好运
您需要通过以下方式之一请求扩展权限,即此处的"电子邮件":
-
php-sdk
使用sdk对象的getLoginUrl()方法,并在"scope"中指定所需的扩展权限,并将用户重定向到其返回值。此外,您还需要在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); }
-
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()
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);
}