admin管理员组文章数量:1434949
I have a photo gallery on my website and I want to require facebook authentication in order to view it and collect names/email addresses. So far I have went [here][1] and read that I tried the php example which worked but I am confused on how to get the users name/email when authenticating? I used this but in the example why would "code" be empty and how can I get the user's info like how in the example its getting the "state". Also in my version it just fires off the redirect, how e in the example the 2nd condition is never met?
The example:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = @file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
I have a photo gallery on my website and I want to require facebook authentication in order to view it and collect names/email addresses. So far I have went [here][1] and read that I tried the php example which worked but I am confused on how to get the users name/email when authenticating? I used this but in the example why would "code" be empty and how can I get the user's info like how in the example its getting the "state". Also in my version it just fires off the redirect, how e in the example the 2nd condition is never met?
The example:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook./dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook./oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = @file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook./me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
Share
Improve this question
asked Mar 1, 2012 at 5:32
Haroon KhalidHaroon Khalid
11 silver badge1 bronze badge
2 Answers
Reset to default 3For getting user e-mail, you need to give required permission like email (more)
Try this..
require_once "facebook.php";
$facebook = new Facebook(array('appId' => '[YOUR_APP_ID]', 'secret' => '[YOUR_SEC_ID]'));
$user_fb = $facebook->getUser();
if($user_fb == 0)
{
$user_fb = $facebook->getUser();
}
if ($user_fb) // Check user's FB user ID has getting or not
{
$user_profile = $facebook->api('/me');
$logoutUrl = $facebook->getLogoutUrl();
echo $user_profile['email'];
echo $user_profile['first_name'];
echo $user_profile['last_name'];
}
else // user's FB user ID has not getting load login url with email permission
{
$perms = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($perms);
echo "<script> top.location.href='" . $loginUrl . "'</script>";
}
Download Package
Reread the authentication docs and maybe some tutorials on OAuth.
When you have app_id
, app_secret
and my_url
set up correctly the redirect will take you to a facebook. authorisation page, and on authorising the app to access your data via the API you'll be redirected back with code
non-empty.
本文标签: How to get user email from facebook authentication example via php or javascriptStack Overflow
版权声明:本文标题:How to get user email from facebook authentication example via php or javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745629757a2667198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论