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
Add a ment  | 

2 Answers 2

Reset to default 3

For 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