无法保存电子邮件和图片 FB OAuth
Not able to save EMail and Image FB OAuth
我正在使用 FB PHP SDK 4.2 让用户通过 FB 登录访问。除了两个问题外,一切都运行良好:
- M 没有收到任何邮件来保存它 DB。
- M 没有得到任何用户图像。
其余用户名和FB社交ID,我可以保存在数据库中。
以下是必需的代码:
FacebookSession::setDefaultApplication(FB_APP_ID, FB_APP_SECRET);
$helper = new FacebookRedirectLoginHelper(FB_REDIRECT_URI);
if(isset($_GET['type']) && $_GET['type'] == 'facebook' ){
$fb_url = $helper->getLoginUrl(array('email'));
header('Location: ' . $fb_url);
}
$session = $helper->getSessionFromRedirect();
if(isset($_SESSION['token'])){
$session = new FacebookSession($_SESSION['token']);
try{
$session->validate(FB_APP_ID, FB_APP_SECRET);
}catch(FacebookAuthorizationException $e){
echo $e->getMessage();
}
}
$data = array();
if(isset($session)){
$_SESSION['token'] = $session->getToken();
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::className());
$data = $graph->asArray();
$id = $graph->getId();
$image = "https://graph.facebook.com/".$id."/picture?width=100";
$data['image'] = $image;
if($user_obj->fb_login($data)){header('Location: ../');}
else{header('Location: ../');}
}
fb_login function is used to save data in database
为了检索当前登录用户的电子邮件,用户需要向您的应用授予 email
权限。只有这样你才能取回它。您可以阅读有关权限的更多信息 here.
此外,在 v2.4 中,您只会获得最少的字段集,对于 email
字段,您需要在 API 调用中明确指定。
In the past, responses from Graph API calls returned a set of default
fields. In order to reduce payload size and improve latency on mobile
networks we have reduced the number of default fields returned for
most Graph API calls. In v2.4 you will need to declaratively list the
response fields for your calls.
所以在 v2.4 中,您需要明确指定字段。例如
https://graph.facebook.com/me?fields=name,email&access_token=<user-access-token>
我正在使用 FB PHP SDK 4.2 让用户通过 FB 登录访问。除了两个问题外,一切都运行良好:
- M 没有收到任何邮件来保存它 DB。
- M 没有得到任何用户图像。
其余用户名和FB社交ID,我可以保存在数据库中。
以下是必需的代码:
FacebookSession::setDefaultApplication(FB_APP_ID, FB_APP_SECRET);
$helper = new FacebookRedirectLoginHelper(FB_REDIRECT_URI);
if(isset($_GET['type']) && $_GET['type'] == 'facebook' ){
$fb_url = $helper->getLoginUrl(array('email'));
header('Location: ' . $fb_url);
}
$session = $helper->getSessionFromRedirect();
if(isset($_SESSION['token'])){
$session = new FacebookSession($_SESSION['token']);
try{
$session->validate(FB_APP_ID, FB_APP_SECRET);
}catch(FacebookAuthorizationException $e){
echo $e->getMessage();
}
}
$data = array();
if(isset($session)){
$_SESSION['token'] = $session->getToken();
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::className());
$data = $graph->asArray();
$id = $graph->getId();
$image = "https://graph.facebook.com/".$id."/picture?width=100";
$data['image'] = $image;
if($user_obj->fb_login($data)){header('Location: ../');}
else{header('Location: ../');}
}
fb_login function is used to save data in database
为了检索当前登录用户的电子邮件,用户需要向您的应用授予 email
权限。只有这样你才能取回它。您可以阅读有关权限的更多信息 here.
此外,在 v2.4 中,您只会获得最少的字段集,对于 email
字段,您需要在 API 调用中明确指定。
In the past, responses from Graph API calls returned a set of default fields. In order to reduce payload size and improve latency on mobile networks we have reduced the number of default fields returned for most Graph API calls. In v2.4 you will need to declaratively list the response fields for your calls.
所以在 v2.4 中,您需要明确指定字段。例如
https://graph.facebook.com/me?fields=name,email&access_token=<user-access-token>